计算出现最大数量的次数

时间:2012-12-12 03:28:23

标签: c

我无法弄清楚如何计算输入最大数字的次数。请帮忙。如果我将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 个答案:

答案 0 :(得分:2)

您需要将times重置为1

if(m == max) {
    times++;
} else if(m > max)
    max = m;
    times = 1;
}

并将其初始化为1

int times = 1, n, m, i, max;