计算变量更改了多少次
Counting how many times variable has changed
我必须编写程序,当终止无限而循环"while(cin>>a)"时,假设"-1",程序告诉我值增加了多少倍。对于输入"0 0 2 2 3 4 8 8 8 8 -1",它应该打印"4"。第一部分不是问题,但我不知道如何计算它随着时间的推移发生了多少次变化。有什么提示吗?多谢。
您应该使用计数器,每次值增加时,计数器基本上都会增加。检查下面的代码:
int value, highestValue, counter = 0, counter2 = 0;
do{
cout << "Enter the value: ";
cin >> value;
if(counter2 == 0){
highestValue = value;
}
if(value > highestValue){
counter++;
highestValue = value;
}
counter2++;
}while(value != -1);
cout << "The number increased " << counter << " times!n";
第一个 if 语句中需要第二个计数器 (counter2) 来存储您输入的第一个值作为最大值。
int p = -1, k, a, b;
while( cin >> a ) //infinite loop
{
if ( k != a )
p++;
b = a - k;
if(a=-1)
exit(0);
k = a;
}
cout << "value increased by" << b;
cout << "number of times it has changed over time = " << p;
相关文章:
- 在 C++ AMP 数组中复制数据多少次?
- C++ 检查结果数组中有多少次数字
- 我如何使它,无论用户用空白字符串按 Enter 多少次,它总是打印"开始"字符串?
- 如何计算整数链中使用了多少次数字?
- 如果函数按值传递并按值返回,将调用复制构造函数多少次
- 复制构造函数被调用了多少次?
- 此函数调用了多少次
- 我需要声明多少次 std::ios::sync_with_stdio(假);在程序中
- 尝试创建一个读取.txt文件,显示它,计数唯一单词的程序,并在使用了多少次的情况下显示独特的单词.C
- 算法 - 集合中出现了多少次
- GCC通过输入源代码进行了多少次通过
- 根据输入文本对单词进行排序,具体取决于重复多少次
- 假设没有编译器优化,则将创建多少次此对象
- 如何计算在Sentinel Loop C 中输入多少次输入
- MPI_REDUCE调用的减少操作是多少次
- 数组在此代码中复制了多少次
- 我怎么能知道13号星期五在一年中出现了多少次
- Q_IMPORT_PLUGIN,我可以使用多少次
- 在递归函数中打印一次变量,该变量会随着每次递归而不断变化
- 计算变量更改了多少次