c++因子程序:输出给定因子数
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 <<"有"<<"因素"。& lt; & lt;endl;"。我不确定如何计算程序输出的因子数量。
例如:7的因数是
1 7有两个因素。
我该如何计算和输出这个例子中的"2"呢?
非常感谢帮助。
代替
cout << "There are " << " factors." << endl;
使用cout << "There are " << counter << " factors." << endl;
如果你这样做,你必须移动你定义counter
的那一行。
它不是main
中的第一行,而是需要移动到while
循环中的第一行。
相关文章:
- 递归函数计算序列中的平方和(并输出过程)
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 请解释"函数1(p1,p2,p3);"的输出
- C++:将控制台输出存储在宏中更好吗
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 如何在OMNET++中指定与命令行参数组合的输出文件名
- 为什么我的代码在输出中增加了93天
- 如何从void函数输出字符串
- 输入到文件并输出到另一个文件,并将流文件传递给函数
- AES加密到解密未正确输出
- 如何将c++程序的一些输出传递给shell,以便在shell中使用
- 使用C++程序合并排序没有得到正确的输出
- 为什么我不能在不创建字符串变量的情况下使用函数的字符串输出
- C++格式化输出问题
- 将值从二维数组输出到文本文件
- 集合上的输出迭代器:assign和increment迭代器
- 在while循环中输入带有std::cin的字符串后,控制台会输出大量胡言乱语
- 为什么rk4.do_step不输出C++中的初始条件
- 输出没有重复元素的动态数组(收缩数组)C++
- 如何将"sort"程序的输出从子程序重定向到父程序