如何计算每个数字遇到了多少次

How to count how many times each number has been encountered?

本文关键字:数字 遇到 多少次 何计算 计算      更新时间:2023-10-16

我正在尝试编写一个程序来计算程序遇到的每个数字。通过将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++;
    }


}

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

让我们一步一步地复习。

输出错误的实际原因是,当需要迭代数组的第一个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;
}