繁体   English   中英

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

[英]How to find the number of occurrences of each digit in a random integer?

我制作了一个简单的随机程序,可以生成 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;
}

下一阶段是设置一个 integer 数组来计算每个数字出现的次数。 例如,如果随机数是 144566888,那么 output 将是

1 appears 1 time 
4 appears 2 times
5 appears 1 time
6 appears 2 times
8 appears 3 times

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

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;
}

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

这样的事情将是一个好的开始:

#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";
        }
    }
}

正如预期的那样,运行几次的 output 是:

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.

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

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

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

// your rng-based digits in an array:
int a[9]={1,4,4,5,6,6,8,8,8};

// For checking unique/distinct digits first, and later on to hold the frequency as transferred by count:
int frequency[9]={-1,-1,-1,-1,-1,-1,-1,-1,-1};

    // Our counter variable, but counts will be transferred to frequency[] later on:
    int count;

    for(int i=0; i<9; i++)
    {   // Each distinct element occurs once atleast so initialize to one:
        count = 1;
        for(int j=i+1; j<9; j++)
        {
            // If dupe is found: 
            if(a[i]==a[j])
            {
                count++;

                // Ensuring not to count frequency of same element again:
                frequency[j] = 0;
            }
        }

        // If frequency of current element is not counted:
        if(frequency[i] != 0)
            frequency[i] = count;
    }

    for(int i=0; i<9; i++)
    {
        if(frequency[i] != 0)
            printf("%d appears %d times\n", a[i], frequency[i]);
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM