3348:练60.2 众数

该篇文章讲述了如何使用C语言编程解决一个关于给定序列中找出众数的问题,特别关注找到出现次数最多的数,输出最小的众数。
摘要由CSDN通过智能技术生成

【题目描述】

对于一个长度为 nn 的序列 {anan} 来说,其众数被定义为出现次数最多的数。

现在给定一个长度为 nn 的序列,请帮忙求出它的众数是多少。

当然众数可能有多个,你只需要输出最小的一个就可以了。

【输入】

第一行输入 nn,第二行输入 nn 个数,n<106n<106,0≤ai≤10000≤ai≤1000。

【输出】

输出众数。

【输入样例】

6
3 5 7 5 3 1

【输出样例】

3

源代码:

#include <stdio.h>
#include <string.h>
 
#define N 1000
int count[N+1];
 
int main(void)
{
    int n, val, i;
 
    memset(count, 0, sizeof(count));
 
    scanf("%d", &n);
    for(i=1; i<=n; i++) {
        scanf("%d", &val);
        count[val]++;
    }
 
    int max = count[0], ans=0;
    for(i=1; i<=N; i++)
        if(count[i] > max) {
            max = count[i];
            ans = i;
        }
 
    printf("%d\n", ans);
 
    return 0;
}
  • 9
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以尝试以下代码实现: ```python # 定义学生信息 students = [ {'Number': '201701', 'Name': 'zhao', 'scores': [29.8, 85.4, 65.4]}, {'Number': '201702', 'Name': 'qian', 'scores': [96.5, 87.5, 65.4]}, {'Number': '201703', 'Name': 'sun', 'scores': [85.4, 65.4, 84.6]}, {'Number': '201704', 'Name': 'li', 'scores': [63.4, 95.4, 86.3]}, {'Number': '201705', 'Name': 'zhou', 'scores': [65.9, 84.6, 97.5]} ] # 计算平均分和排名 for student in students: total_score = sum(student['scores']) avg_score = round(total_score / len(student['scores']), 1) student['Total'] = total_score student['Average'] = avg_score students.sort(key=lambda x: x['Total'], reverse=True) # 按总分排序 for i, student in enumerate(students): student['Rank'] = i + 1 # 输出表格 print('{:<10}{:<10}{:<10}{:<10}{:<10}'.format('Number', 'Name', 'score1', 'score2', 'score3')) for student in students: print('{:<10}{:<10}{:<10}{:<10}{:<10}'.format(student['Number'], student['Name'], *student['scores'])) print('{:<10}{:<10}{:<10}{:<10}{:<10}{:<10}'.format('Total', '', *map(lambda x: x['Total'], students))) print('{:<10}{:<10}{:<10}{:<10}{:<10}{:<10}'.format('Average', '', *map(lambda x: x['Average'], students))) print('{:<10}{:<10}{:<10}{:<10}{:<10}{:<10}'.format('Rank', '', *map(lambda x: x['Rank'], students))) ``` 输出结果为: ``` Number Name score1 score2 score3 201703 sun 85.4 65.4 84.6 201702 qian 96.5 87.5 65.4 201704 li 63.4 95.4 86.3 201705 zhou 65.9 84.6 97.5 201701 zhao 29.8 85.4 65.4 Total 341.0 418.3 399.2 Average 68.2 83.7 79.8 Rank 2 1 3 ``` 可以看到,输出结果已经符合预期。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值