统计最大数出现的次数

标签 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:

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/

相关文章:

C:打印的字符不正确,怀疑是内存。问题

C - 整数之间 2 个变量的组合 [ARRAY]

c - 使用 PIC 18F4550 使 3 个 LED 闪烁

c - 使用 fopen 创建所有需要的文件夹

处理集合问题的C程序

c - Linux 中的终端屏幕快捷方式

c - 在 ubuntu 中执行程序时出现 C 段错误

c - JNI——带有 bool 值的 C 实现

c - SPARC 程序集,设置 cflags

c++ - R 调用 C 代码比 C++ 函数调用 C 代码更快?