【问题标题】:C++ Check how many times there are numbers in the resulting arrayC ++检查结果数组中有多少次数字
【发布时间】:2020-06-11 15:44:34
【问题描述】:

我是 C++ 初学者 有人可以帮助我。 INT 数组。随机。 用 10 到 77 的随机数填充 1000 个整数的数组。 检查结果数组中有多少次有数字 10、20、30、40、50、60、70。 打印收到的统计信息。

我看到它是这样的,但我不知道如何计算数组中的数字。

int main()
{
    int number[1000];
    int countUnits[7];
    int count = 0;
    srand(time(NULL));
    for (int i = 0; i < 1000; i++) {
        number[i] = rand() % 68 + 10;

    }
    for (int i = 0; i < 1000; i++) {
        if (number[i] % 10 == 0) {

        }
    }
}

【问题讨论】:

  • 探索 std::count_if 函数并将其与 lambda 谓词耦合。
  • @Ron 你能用这个吗?看起来它返回单个/总数。但是,由于描述和他的int countUnits[7];,OP 似乎需要单独的计数(即直方图)
  • 大声思考:假设您知道您当前正在查看的数字是 40(忘记您是如何知道的,您计算出来并且知道它是 40)。 countUnits 数组中的哪个槽被保留用于添加找到的 40 的数量?

标签: c++


【解决方案1】:

就像只数一个数字一样开始,除了你有七张支票和七个计数器:

for (int i = 0; i < 1000; i++)
{
    if (number[i] == 10)
        countUnits[0] += 1;
    else if (number[i] == 20)
        countUnits[1] += 1;
    else if ...
...
}

然后你注意到测试和索引有一个模式,并简化:

for (int i = 0; i < 1000; i++)
{
    if (number[i] % 10 == 0)
        countUnits[number[i]/10-1] += 1;
}

或者,如果你想用你的 m4d sk1llz 混淆别人:

for (int i = 0; i < 1000; i++)
{
    countUnits[number[i]/10-1] += number[i] % 10 == 0;
}

【讨论】:

  • 这是错误的。如果i 是 934,那么计数桶索引将为 92。请参阅下面的答案
  • @CraigEstey 是的,我有一个错误。
  • 我完成了这个小程序。非常感谢您的帮助。我在这方面有更多经验。
【解决方案2】:

countUnits 的索引必须基于 number[i] 的值。

并且不是 i 正如其他人所建议的那样。有了这个(例如)如果索引 i 是 934 并且可以被 10 整除,那么结果索引将为 92

这是代码(请原谅printf):

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int
main(void)
{
    int number[1000];
    int countUnits[7] = { 0 };

    srand(time(NULL));

    for (int i = 0; i < 1000; i++) {
        number[i] = rand() % 68 + 10;
    }

    for (int i = 0; i < 1000; i++) {
        int value = number[i];
        if (value % 10 == 0)
            countUnits[value / 10 - 1] += 1;
    }

    for (int i = 0; i < 7; i++)
        printf("%d: %d\n",(i + 1) * 10,countUnits[i]);

    return 0;
}

【讨论】:

    【解决方案3】:

    你不远。但是你应该先初始化countUnits

    int countUnits[7] = {0};    // enough to zero initialize the whole array
    

    然后你可以数数:

    for (int i = 0; i < 1000; i++) {
        if (number[i] % 10 == 0) {
            countUnits[number[i] / 10 - 1] += 1;
        }
    }
    

    这样你只扫描一次数组。

    【讨论】:

    • 这是错误的。如果i 是 934,那么计数桶索引将为 92。请参阅下面的答案
    • @CraigEstey:我的错。感谢 MichalH 修正了错字。
    【解决方案4】:

    因为这是 C++,所以它应该更简单。它读起来更像 Python 而不是 C。在这种情况下,这是一件好事!

    我是这样写的:

    #include <array>
    #include <iostream>
    #include <map>
    #include <random>
    
    int main()
    {
        std::random_device rd;  //Will be used to obtain a seed for the random number engine
        std::mt19937 gen(rd()); //Standard mersenne_twister_engine seeded with rd()
        std::uniform_int_distribution<> distrib(10, 77);
    
        std::array<int, 1000> numbers;
        for (auto &number : numbers)
            number = distrib(gen);
    
        std::map<int, int> counts;
        for (auto const &number : numbers)
            if ((number % 10) == 0)
                counts[number] ++;
    
        for (auto const &number_count : counts)
        {
            auto number = number_count.first;
            auto count = number_count.second;
            std::cout << number << " found " << count
                << (count == 1 ? " time" : " times") << '\n';
        }
    }
    

    样本输出:

    10 found 13 times
    20 found 15 times
    30 found 13 times
    40 found 7 times
    50 found 15 times
    60 found 9 times
    70 found 17 times
    

    【讨论】:

      猜你喜欢
      • 2021-08-11
      • 1970-01-01
      • 1970-01-01
      • 2020-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-16
      • 2012-02-07
      相关资源
      最近更新 更多