(c语言)找出最大数,并且计算其出现次数。

(c语言)编写程序读取正整数,找出他们的最大数,然后计算最大数出现的次数。输入以-1结束。

输入示例:2 3 2 1 3 9 88 7 9 10 88 3 2 -1
输出示例:max=88 count=2

这个大体思路是怎样的呢,初学不久。

供参考:

#include <stdio.h>
#include <limits.h>
int main()
{
    int n, max = INT_MIN, count = 0;
    while (scanf("%d", &n) == 1 && n != -1) {
        if (max <= n) {
            if (max == n) {
                count++;
            }
            else {
                max = n; 
                count = 1;
            }
        }
    }
    printf("max=%d count=%d", max, count);
    return 0;
}

简单处理就是先循环一遍找出最大数,再循环一遍找出最大数的数量


#include<iostream>
#include <stdio.h>

int main(){
    int a[100];
    int x;
    int count=0;
    int max=0;
    int i=0;
    while (scanf("%d",&x))
    {
        if(x==(-1))
            break;

        if(max<x)
            max = x;
        a[i] = x;
        i++;
    }
    for(int j=0;a[j]!=(-1);j++)
        if(a[j]==max)
            count++;
    printf("max=%d count=%d\n",max,count);
    system("pause");
    return 0;
}

截图:

img