[英]C++ Factor Program: Outputting Number of Factors Given
我正在編寫一個簡單的程序,通過 Linux 重定向查找整數列表的因子。 我快完成了,但我被困在了一個部分。 到目前為止,這是我的程序:
#include<iostream>
using namespace std;
int main()
{
int counter = 0;
int factor;
cin >> factor;
while (cin)
{
if (factor < 0)
break;
cout << "The factors of " << factor << " are " << endl;
for(int i=factor; i>=1; i--)
if (factor % i == 0)
{
counter++;
cout << i << endl;
}
cout << "There are " << " factors." << endl;
cout << endl;
cin >> factor;
}
return 0;
}
現在我遇到的問題是“cout <<”有“<<”因素。“<<endl;”。 我不確定如何計算程序輸出的因子數。
例如:
7的因數是
1
7
有2個因素。
在這個例子中,我將如何計算和輸出“2”。
非常感謝幫助。
代替
cout << "There are " << " factors." << endl;
用
cout << "There are " << counter << " factors." << endl;
如果這樣做,則必須移動定義counter
的行。
它不是main
中的第一行,而是需要移動到while
循環中的第一行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.