我不知道如何计算输入最大数字的次数。请帮忙。如果我将 s 初始化为 0,则不会计算第一个数字(如果它是最高的)。
#include <stdio.h>
int main (void)
{
int times=0,n,m,i,max;
printf("How many numbers(n) you going to enter:\n");
scanf("%d",&n);
printf("Enter the numbers:\n");
scanf("%d",&m);
max=m;
for(i=1;i<=n;i++)
{
scanf("%d",&m);
if(m==max)
times++;
if(m>max)
max=m;
}
printf("The Largest Number is %d and was entered %d times",max , times);
return 1;
}
最佳答案
您需要将次
重置为1
:
if(m == max) {
times++;
} else if(m > max)
max = m;
times = 1;
}
并将其初始化为1
:
int times = 1, n, m, i, max;
关于统计最大数出现的次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13832288/