用C语言编写 统计侯选人选票:有3个候选人,每个选民只能投票选一人,要求编一个统计选票的程序,先后输入被选人的名字,最后输出各人得票结果。
时间: 2023-04-11 16:00:45 浏览: 297
这是一个技术问题,我可以回答。可以使用C语言中的数组和循环结构来实现统计选票的程序。首先定义一个包含3个元素的数组,分别表示3个候选人的得票数。然后使用循环结构,对每个选民输入的投票进行统计,最后输出各人得票结果。具体实现可以参考以下代码:
```
#include <stdio.h>
int main()
{
char candidates[3][20] = {"Candidate A", "Candidate B", "Candidate C"};
int votes[3] = {0};
int voter_num, candidate_num;
printf("Enter the number of voters: ");
scanf("%d", &voter_num);
for (int i = 0; i < voter_num; i++) {
printf("Enter the number of candidate (1-3): ");
scanf("%d", &candidate_num);
votes[candidate_num - 1]++;
}
printf("The result of the election:\n");
for (int i = 0; i < 3; i++) {
printf("%s: %d votes\n", candidates[i], votes[i]);
}
return 0;
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)