【发布时间】:2019-08-12 04:32:42
【问题描述】:
我想打印第一个数到4的数字,例如我有这个随机函数,我想查看第一个达到4次的数字。所以这是第一个打印自己 4 次的数字。例如:
int n;
int count1 = 0;
int count2 = 0;
int count3 = 0;
while (true) {
cout << "Enter a number btween 1-3" << endl;
cin >> n;
if (n == 1) {
count1++;
}
if (n == 2) {
count2++;
}
if (n == 3) {
count3++;
}
if (count1 == 4) {
cout << "You printed the number 1 4 times!";
break;
}
if (count2 == 4) {
cout << "You printed the number 2 4 times!";
break;
}
if (count3 == 4) {
cout << "You printed the number 3 4 times!";
break;
}
但是如果是 1-1000 个数字而不仅仅是 1-3,我会怎么做呢?
我想这样做,但在一个随机函数上 - 该数字的计数是 4 次的第一个数字打印该数字 -
int fun() {
srand(time(NULL));
return rand() % 3;
}
然后我想在 main 中执行第一个达到例如 4 次的数字打印这个数字。
我试着做这样的事情:
for (int i = 0; i < 31; i++) {
arr[fun()]++;
cout << arr[fun()];
if (arr[fun()] == 4) {
cout << arr[fun()];
}
}
【问题讨论】:
-
如果是 1000 个数字,您将使用 for 循环。
-
@Axium 你错过了整点
-
我不回答,我建议。
-
他使用了一个数组,这需要与向量几乎相同的工作量。我只是建议一些代码。
-
看,这家伙切换到矢量了。