码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 7-3 投票统计 武汉理工大学C语言


    7-3 投票统计
    分数 15
    作者 TracyLi
    单位 成都信息工程大学
    用程序模拟一个活动的投票统计功能。首先输入参选人员个数,再输入每位参选人员名字(不超过20字节),再输入选票张数,再依次输入选票中所选的参选人名(选票中必须选参选中的其中一位)。在输入选票过程中统计每位参选人的得票数,最终按得票数由高到低的顺序输出参选人和其票数(空格分隔),如果票数相同,则按名字从小到大的顺序(ASCII码顺序)输出。
    提示:选票信息按如下结构定义:

    struct vote
    {
    char name[20];//名字
    int count;//票数
    };
    输入样例:
    3
    Li
    Wang
    Zhang
    8
    Li
    Wang
    Li
    Zhang
    Li
    Li
    Wang
    Zhang
    输出样例:
    Li 4
    Wang 2
    Zhang 2

    #include 
    #include 
    #include 
    
    struct Vote {
        char name[20];
        int count;
    };
    
    int compare_votes(const void *a, const void *b) {
        const struct Vote *vote_a = (const struct Vote *)a;
        const struct Vote *vote_b = (const struct Vote *)b;
        
        if (vote_a->count == vote_b->count) {
            return strcmp(vote_a->name, vote_b->name);
        }
        return vote_b->count - vote_a->count;
    }
    
    int main() {
        int num_candidates, num_votes;
        scanf("%d", &num_candidates);
        
        struct Vote *candidates = (struct Vote *)malloc(num_candidates * sizeof(struct Vote));
        for (int i = 0; i < num_candidates; i++) {
            scanf("%s", candidates[i].name);
            candidates[i].count = 0;
        }
    
        scanf("%d", &num_votes);
    
        for (int i = 0; i < num_votes; i++) {
            char selected_name[20];
            scanf("%s", selected_name);
            for (int j = 0; j < num_candidates; j++) {
                if (strcmp(selected_name, candidates[j].name) == 0) {
                    candidates[j].count++;
                    break;
                }
            }
        }
    
        qsort(candidates, num_candidates, sizeof(struct Vote), compare_votes);
    
        for (int i = 0; i < num_candidates; i++) {
            printf("%s %d\n", candidates[i].name, candidates[i].count);
        }
    
        free(candidates);
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
  • 相关阅读:
    JavaScript之正则表达式的使用方法详细介绍
    GameFramework:Resource加载,资源加载,依赖加载,任务池,对象池,引用计数
    y95.第六章 微服务、服务网格及Envoy实战 -- Front Proxy and TLS(六)
    Redis源码之SDS简单动态字符串
    Python统计学08——一元线性回归
    WiFi模块引领零售数字化转型:智能零售体验再定义
    Open-source software (OSS)
    使用 gpg 对Linux下的文件加密
    JG/T 543-2018 铝塑共挤门窗检测
    Redis中 HyperLogLog数据类型使用总结
  • 原文地址:https://blog.csdn.net/m0_63746713/article/details/133867059
  • 最新文章
  • 【JVM】编译执行与解释执行的区别是什么?JVM 使用哪种方式?
    用 Hashids 优雅解决 C 端自增 ID 暴露问题
    V8引擎 精品漫游指南--Ignition篇(上) 指令 栈帧 槽位 调用约定 内存布局 基础内容
    LLVM Pass快速入门(四):代码插桩
    milkup:桌面端 markdown AI续写和即时渲染
    基于项目工程构建SBOM(软件物料清单)的研究
    鸿蒙应用开发UI基础第二节:鸿蒙应用程序框架核心解析与实操
    .NET 中如何快速实现 List 集合去重?
    扣子Coze实战:从0到1打造抖音+小红书热点监控智能体
    浅谈数据访问层
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号