【问题标题】:How to count how many times each number has been encountered?如何计算每个数字遇到了多少次?
【发布时间】:2015-08-17 03:43:10
【问题描述】:

我正在尝试编写一个程序来计算程序遇到的每个数字。通过将 M 作为数组元素数量的输入,Max 是最大数量的数字,例如在 M[i] 中写入输入时不应超过此数字。出于某种原因,当我输入像

这样的小输入时,程序运行良好

数据输入:

10 3
1 2 3 2 3 1 1 1 1 3

答案:

5 2 3

但是当我为数组元素输入一个大输入(如 364)和最大输入为 15 时。输出没有按预期工作,我找不到原因!

#include "stdafx.h"
#include <iostream>
#include<fstream>
#include<string>
#include <stdio.h>
#include<conio.h>
using namespace std;

int  ArrayValue;
int Max;
int M[1000];
int checker[1000];
int element_cntr = 0;
int cntr = 0;
int n = 0;
void main()
{

    cout << "Enter the lenght of the Elements, followed by the maximum number: " << endl;
    cin >> ArrayValue>> Max;

    for (int i = 0; i < ArrayValue; i++)
    {
        cin >> M[i];
        checker[i]= M[i] ;
        element_cntr++;

        if (M[i] > Max)
        {
            cout << "the element number " << element_cntr << " is bigger than " << Max << endl;
        }


    }


    for (int i = 0; i < Max; i++)
    {
        cntr = 0;
        for (int j = 0; j < ArrayValue; j++)
        {

            if (M[n] == checker[j])
            {
                cntr+=1;

            }       

        }


        if (cntr != 0)
        {
            cout << cntr << " ";
        }
        n++;
    }





}

【问题讨论】:

标签: c++ arrays loops if-statement


【解决方案1】:

您有一般算法问题和几个代码问题,这些问题使代码难以维护、不可读和混乱。这就是为什么你不明白为什么它不起作用。

让我们一步一步地回顾它。

不正确输出的实际原因是当您需要遍历第一个Max 整数时,您只遍历数组的第一个Max 项。例如,让我们有输入:

7 3
1 1 1 1 1 2 3

虽然正确答案是:5 1 1,但您的程序将输出 5 5 5,因为在输出循环中它将遍历前三个项目并为它们输出:

 for (int i = 0; i < Max; i++)
    for (int j = 0; j < ArrayValue; j++)
        if (M[n] == checker[j]) // M[0] is 1, M[1] is 1 and M[2] is 1

它将输出初始数组的前三项的答案。在您的示例中,它运行良好,因为前三个项目是 1 2 3
为了使其工作,您需要将您的条件更改为

if (n == checker[j]) // oh, why do you need variable "n"? you have an "i" loop!
{
    cntr += 1;
}    

它会起作用,但你的代码和算法都绝对不正确......

不是那种合适的解决方案

您有一个不必要的变量element_cntr - 循环变量i 将提供相同的值。你在复制它的价值。

此外,在您的输出循环中,您创建了一个变量n,而您有一个循环变量i,其作用相同。您可以安全地删除变量n 并将if (M[n] == checker[j]) 替换为if (M[i] == checker[j])

此外,如果变量M,您的checker 数组是完整副本。为什么你喜欢复制所有的值? :)

您的代码至少应该是这样的:

using namespace std;

int ArrayValue;
int Max;
int M[1000];
int cntr = 0;

int main()
{

    cout << "Enter the lenght of the Elements, followed by the maximum number: " << endl;
    cin >> ArrayValue >> Max;

    for (int i = 0; i < ArrayValue; i++)
    {
        cin >> M[i];

        if (M[i] > Max)
        {
            cout << "the element number " << i << " is bigger than " << Max << endl;
        }
    }


    for (int i = 0; i < Max; i++)
    {
        cntr = 0;
        for (int j = 0; j < ArrayValue; j++)
        {
            if (i == M[j])
            {
                cntr ++;
            }      
        }

        if (cntr != 0)
        {
            cout << cntr << " ";
        }
    }

    return 0;
}

适当的解决方案

为什么需要嵌套循环?您采用O(n*m) 操作来计算项目的出现次数。通过O(n) 操作可以轻松计算。

一边阅读一边数数:

using namespace std;

int arraySize;
int maxValue;
int counts[1000];

int main()
{

    cout << "Enter the lenght of the Elements, followed by the maximum number: " << endl;
    cin >> arraySize >> maxValue;

    int lastReadValue;

    for (int i = 0; i < arraySize; i++)
    {
        cin >> lastReadValue;

        if (lastReadValue > maxValue)
            cout << "Number " << i << " is bigger than maxValue! Skipping it..." << endl;
        else
            counts[lastReadValue]++; // read and increase the occurence count
    }


    for (int i = 0; i <= maxValue; i++)
    {
        if (counts[i] > 0)          
            cout << i << " occurences: " << counts[i] << endl; // output existent numbers
    }

    return 0;
}

【讨论】:

  • 哦。我永远不会理解 StackOverflow。我写了一个 110 行的答案以获得反对票和最佳答案标记。任何解释,否决者?
  • 为什么输出显示 Maxvalue-1 它只打印正确的输出而不是正确的数量
  • @haithamhany 老实说,我不明白你的问题。 “输出正确但数量不正确”是什么意思?
  • 我的意思是当你尝试我的简单例子时,它需要 10 3 1 2 3 2 3 1 1 1 1 3 输出是 5 2 而不是 5 2 3 等等
  • @haithamhany 哦,现在我明白了。更新了答案。最后一个输出循环应该是for (int i = 0; i &lt;= maxValue; i++),但不是for (int i = 0; i &lt; maxValue; i++)。由于这个错字,它没有输出最后一个Max 值的答案:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多