2026/1/7 18:40:34
网站建设
项目流程
word网站超链接怎么做,盘锦949公社最新招聘,网店网站建设哪家,免费的会计做账系统用程序模拟一个活动的投票统计功能。首先输入参选人员个数#xff0c;再输入每位参选人员名字#xff08;不超过20字节#xff09;#xff0c;再输入选票张数#xff0c;再依次输入选票中所选的参选人名#xff08;选票中必须选参选中的其中一位#xff09;。在输入选票…用程序模拟一个活动的投票统计功能。首先输入参选人员个数再输入每位参选人员名字不超过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 iostream#include cstringusing namespace std;struct vote {char name[20];int count 0; // 直接初始化为0};int main() {int n, m;cin n;vote candidates[100];// 输入候选人for (int i 0; i n; i) {cin candidates[i].name;}cin m;// 统计选票for (int i 0; i m; i) {char name[20];cin name;// 查找这个人for (int j 0; j n; j) {if (strcmp(candidates[j].name, name) 0) {candidates[j].count;break;}}}// 简单排序冒泡排序for (int i 0; i n - 1; i) {for (int j 0; j n - 1 - i; j) {// 先比较票数if (candidates[j].count candidates[j 1].count) {// 交换两个候选人vote temp candidates[j];candidates[j] candidates[j 1];candidates[j 1] temp;}// 如果票数相同比较名字else if (candidates[j].count candidates[j 1].count) {if (strcmp(candidates[j].name, candidates[j 1].name) 0) {vote temp candidates[j];candidates[j] candidates[j 1];candidates[j 1] temp;}}}}// 输出结果for (int i 0; i n; i) {cout candidates[i].name candidates[i].count endl;}return 0;}