我正在尝试编写的程序允许我输入 10 个数字,它应该告诉我数字 X 重复 X 次,依此类推。
我一直在尝试这个,但问题是我得到的结果如下:
例如…{1,1,1,1,4,6,4,7,4}
数字 1 重复 4 次
数字 1 重复 3 次
数字 1 重复 2 次
数字 1 重复 1 次
数字 4 重复 3 次
数字 6 重复 1 次
数字 4 重复 2 次
数字 7 重复 1 次
数字 4 重复 1 次
问题是它检查下一个数字与以下数字而不跳过它,或者不知道它之前已经写过它
#include <iostream>
#include <string>
using namespace std;
int main() {
int x[10];
for (int i=0;i<10;i++) {
cin>>x[i];
}
for (int i=0;i<9;i++) {
int count=1;
for (int j=i+1;j<10;j++) {
if (x[i]==x[j]) count++;
}
cout<<"The number "<<x[i]<<" is repeated "<<count<<" times"<<"\n";
}
}
原文由 Mahmoud Nabil 发布,翻译遵循 CC BY-SA 4.0 许可协议
预期输出: