如何计算每个数字遇到了多少次
How to count how many times each number has been encountered?
我正在尝试编写一个程序来计算程序遇到的每个数字。通过将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;
}
相关文章:
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- 检查输入是否不是整数或数字
- 编译包含字符串的代码时遇到问题
- 如何(从固定列表中)选择一个数字序列,该序列将与目标数字相加
- 如何用数字处理log(0)
- 最高有效数字侧的第N位
- 如何获取一个数字的前3位
- 查找最接近的大于当前数字的数字的索引
- 遇到此问题时遇到困难:允许用户输入数组的值并使用 for,而循环也输出输入的最大数字
- 在 cpp 中使用较大数字时遇到大数字的问题<iomanip>
- C++ - 我在输出时遇到问题,因为我正在收到数字和字母
- 在此任务上遇到麻烦,我们需要使用指针和数组来找到文本文件中某些数字的平均值,中值和模式
- 我在这个数组代码上遇到了问题,它应该输出用户之前输入的所有数字
- 在打印出动态分配的数组中的前两个数字时遇到问题,其他数字工作正常
- 为什么我会遇到细分错误?将输出作为数字而不是字符串的原因是什么?
- 仅使用 if else 语句对数字进行排序时遇到问题
- 我在确定数字是否为质数的代码时遇到问题
- 如何计算每个数字遇到了多少次
- 使用DirectShow捕捉数字电视时遇到麻烦