2012-12-12 36 views
0

我無法弄清楚如何統計最大次數輸入的次數。請幫忙。如果我初始化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; 
} 

回答

2

您需要重置times1:它

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

並初始化到1

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

@DavidSchwartz:對不起,我錯過了'最大= M'一部分。 – Ryan

+0

OH YA!多謝你們。猜測我的大腦有點模糊,想到任何邏輯。 –