C++ 如何找到随机整数中每个数字的出现次数?

C++ 如何找到随机整数中每个数字的出现次数?,c++,C++,我制作了一个简单的随机程序,它生成一个0到9之间的随机数 int random_x; int main(int argc, char* argv[]) { srand ( time(NULL) ); for (int t=0;t<10;t++) { random_x = rand() % 10; //generate 10 random integers between 0 and 10 cout << "\n" << random_

我制作了一个简单的随机程序,它生成一个0到9之间的随机数

int random_x;

int main(int argc, char* argv[])
{    
 srand ( time(NULL) );

for (int t=0;t<10;t++)
{ 
    random_x = rand() % 10; //generate 10 random integers between 0 and 10
    cout << "\n" << random_x;
}

这是我尝试过的,但它返回“数字出现0次”

int count[random_x]={0};
while(random_x>0){//循环返回一个数字出现的次数
计数[随机_x%10]+;
random_x=random_x/10;

cout这个想法只是保留一个计数数组(最初都是零),并在生成每个数字时更新它们。然后,在最后,输出这些计数

像这样的事情将是一个良好的开端:

#include <cstdlib>
#include <ctime>
#include <iostream>

int main() {
    srand(time(nullptr));

    // Count accumulators, all originally zero.

    int count[10];
    for (int i = 0; i < 10; ++i) { // or 'int count[10] = {0}' above.
        count[i] = 0;
    }

    // Process random digits, printing and accumulating.

    std::cout << "Generating random digits:";
    for (int i = 0; i < 10; ++i) {
        int digit = rand() % 10;
        std::cout << " " << digit;
        count[digit]++;
    }
    std::cout << "\n";

    // Output the statistics.

    for (int i = 0; i < 10; ++i) {
        if (count[i] == 1) {
            std::cout << "    Digit " << i << " occurs 1 time.\n";
        }
        else if (count[i] > 0) {
            std::cout << "    Digit " << i << " occurs " << count[i] << " times.\n";
        }
    }
}

您的问题与计算数组中每个唯一数字的出现次数有关

对此有不同的方法,其中之一是使用
无序集
(存储唯一值)来存储不同的元素,并计算唯一元素的总数以获得数组大小,这样我们就可以为每个不同的元素分配一个计数器

由于不受时间限制,因此更容易实现两个循环,外部循环遍历所有元素,内部循环首先检查重复(使用数组检查频率状态)并分别计算每个唯一元素的出现次数。(然后将计数值传输到频率保持阵列,并在每次迭代时相应地打印值)

//数组中基于rng的数字:
int a[9]={1,4,4,5,6,6,8,8,8};
//对于先检查唯一/不同数字,然后保持按计数传输的频率:
整数频率[9]={-1,-1,-1,-1,-1,-1,-1,-1,-1};
//我们的计数器变量,但计数稍后将转移到频率[]:
整数计数;

对于(inti=0;ii,在OP给出的输出中,只有不同的元素与它们各自的计数一起被提及
还存储重复次数的计数,因此我想这不是必需的。我运行了此代码,它显示数字出现0次。@David,您似乎没有将其正确地组合到自己的数字中。您添加到问题中的代码将不起作用,因为您需要计算所有随机数,然后输出整个计数无论如何,我对数字的来源有一个误解(一个大数字而不是很多个一位数),所以我现在把它改成了一个完整的独立程序(还有输出)为了更好地向您展示我的意思。请注意,您生成了10个随机数,但在示例中只包含9个。此外,您需要查找数组中的不同元素以及每个元素的计数/频率。@Anirban166如何执行此操作?Im Unsure您可以使用
无序集
存储唯一值和计算集合大小给定的数组中出现的次数。是否受时间复杂性约束?这可以通过两个循环实现,但复杂性是二次的。如果必须使用优化逻辑,我更喜欢第一种方法。(它存储唯一的元素)@Anirban166不,我没有时间限制。好吧,我马上写一个解决方案,等等。
int count[random_x] = {0};

while ( random_x > 0) {  //loop to return how many times a number appears
    count[ random_x % 10]++;
     random_x =  random_x / 10;
     cout <<"Digit occurs: " << random_x << " times" << endl;
}
#include <cstdlib>
#include <ctime>
#include <iostream>

int main() {
    srand(time(nullptr));

    // Count accumulators, all originally zero.

    int count[10];
    for (int i = 0; i < 10; ++i) { // or 'int count[10] = {0}' above.
        count[i] = 0;
    }

    // Process random digits, printing and accumulating.

    std::cout << "Generating random digits:";
    for (int i = 0; i < 10; ++i) {
        int digit = rand() % 10;
        std::cout << " " << digit;
        count[digit]++;
    }
    std::cout << "\n";

    // Output the statistics.

    for (int i = 0; i < 10; ++i) {
        if (count[i] == 1) {
            std::cout << "    Digit " << i << " occurs 1 time.\n";
        }
        else if (count[i] > 0) {
            std::cout << "    Digit " << i << " occurs " << count[i] << " times.\n";
        }
    }
}
Generating random digits: 6 1 5 8 9 2 8 0 5 3
    Digit 0 occurs 1 time.
    Digit 1 occurs 1 time.
    Digit 2 occurs 1 time.
    Digit 3 occurs 1 time.
    Digit 5 occurs 2 times.
    Digit 6 occurs 1 time.
    Digit 8 occurs 2 times.
    Digit 9 occurs 1 time.

Generating random digits: 3 4 0 1 3 3 9 9 5 7
    Digit 0 occurs 1 time.
    Digit 1 occurs 1 time.
    Digit 3 occurs 3 times.
    Digit 4 occurs 1 time.
    Digit 5 occurs 1 time.
    Digit 7 occurs 1 time.
    Digit 9 occurs 2 times.

Generating random digits: 7 7 9 7 3 7 4 3 7 4
    Digit 3 occurs 2 times.
    Digit 4 occurs 2 times.
    Digit 7 occurs 5 times.
    Digit 9 occurs 1 time.