帮我写一个关于班干部选举的c语言程序
时间: 2023-08-20 08:08:47 浏览: 56
好的,以下是一个简单的班干部选举程序:
```c
#include <stdio.h>
int main() {
int num_of_candidates, i;
char candidate_names[10][20];
int candidate_votes[10] = {0}; // 初始化票数为0
printf("请输入候选人数量:");
scanf("%d", &num_of_candidates);
printf("请依次输入候选人姓名:\n");
for (i = 0; i < num_of_candidates; i++) {
scanf("%s", candidate_names[i]);
}
printf("投票开始,请输入候选人编号:\n");
printf("(编号从1开始,输入0表示结束投票)\n");
int vote;
while (1) {
scanf("%d", &vote);
if (vote == 0) {
break;
}
if (vote < 1 || vote > num_of_candidates) {
printf("输入的编号不合法,请重新输入:\n");
continue;
}
candidate_votes[vote - 1]++; // 将相应候选人的票数加1
}
printf("投票结果:\n");
for (i = 0; i < num_of_candidates; i++) {
printf("%s:%d票\n", candidate_names[i], candidate_votes[i]);
}
// 找出得票最多的候选人
int max_votes = candidate_votes[0];
int max_index = 0;
for (i = 1; i < num_of_candidates; i++) {
if (candidate_votes[i] > max_votes) {
max_votes = candidate_votes[i];
max_index = i;
}
}
printf("得票最多的候选人是:%s\n", candidate_names[max_index]);
return 0;
}
```
该程序首先要求用户输入候选人数量和每个候选人的姓名,接着进入投票环节,用户通过输入候选人的编号进行投票。当用户输入0时,投票结束。投票结果会依次输出每个候选人的得票数,以及得票最多的候选人的姓名。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)