C语言问题描述】 在一次选举中,有5位候选人,分别由1-5编号,请编程统计出各位候选人的得票数。 【输入形式】 输入任意多个1-5之间的整数,其它数值表示结束,数据用空格分开。 【输出形式】 见样例输出形式,第1列数值表示候选人编号,第2列数值表示候选人得票数 【样例输入】 1 1 3 2 1 5 3 1 1 2 5 3 4 1 2 3 4 5 2 -1 【样例输出】 1---6 2---4 3---4 4---2 5---3 #include<stdio.h> void mycount(int a[],int b[],int n); int main() { int a[100],b[5]={0},i,x; i=0; scanf("%d",&x); while(x>0&&x<=5) { a[i]=x; scanf("%d",&x); } mycount(a,b,i); for(i=0;i<5;i++) printf("%d---%d\n",i+1,b[i]); return 0; } void mycount(int a[],int b[],int n) { for(int i=0;i<n;i++) }
时间: 2023-12-03 11:47:27 浏览: 86
这段代码存在一些问题,我来帮你修正一下:
```
#include <stdio.h>
void mycount(int a[], int b[], int n);
int main() {
int a[100], b[5] = {0}, i = 0, x;
scanf("%d", &x);
while (x > 0 && x <= 5) {
a[i] = x;
i++;
scanf("%d", &x);
}
mycount(a, b, i);
for (i = 0; i < 5; i++) {
printf("%d---%d\n", i + 1, b[i]);
}
return 0;
}
void mycount(int a[], int b[], int n) {
for (int i = 0; i < n; i++) {
b[a[i] - 1]++;
}
}
```
我对代码进行了以下修改:
1. 在 `mycount` 函数中添加了缺失的大括号;
2. 在 `main` 函数中将 `i` 的初始值设为 0;
3. 在读入数据时,将 `i` 的值加一;
4. 修改了 `mycount` 函数中的计数方式。
这样修改后,程序应该就能正确地统计出各位候选人的得票数了。
相关问题
某次选举,要从五个候选人(编号分别为1、2、3、4、5)中选一名厂长。请编程完成统计选票的工作。c语言实现
好的,以下是一份 C 语言的代码实现:
```c
#include <stdio.h>
int main() {
int votes[5] = {0}; // 初始化选票数组为0
int candidate; // 存储选民投票的候选人编号
int i;
// 循环读取选民的投票,输入-1结束投票
while (1) {
printf("请输入您的投票: ");
scanf("%d", &candidate);
if (candidate == -1) {
break;
}
if (candidate >= 1 && candidate <= 5) {
votes[candidate - 1]++; // 候选人编号从1开始,数组下标从0开始,所以要减1
} else {
printf("无效的候选人编号!\n");
}
}
// 输出各候选人的得票情况
printf("候选人编号\t得票数\n");
for (i = 0; i < 5; i++) {
printf("%d\t\t%d\n", i + 1, votes[i]);
}
// 找出得票最高的候选人
int max_votes = votes[0]; // 初始化最高票数为第一个候选人的票数
int max_candidate = 1; // 初始化最高票数的候选人编号为1
for (i = 1; i < 5; i++) { // 从第二个候选人开始比较
if (votes[i] > max_votes) {
max_votes = votes[i];
max_candidate = i + 1;
}
}
// 输出得票最高的候选人
printf("得票最高的候选人是%d号,得票数为%d。\n", max_candidate, max_votes);
return 0;
}
```
这份代码实现了从控制台读取选民投票,统计各候选人得票情况,找出得票最高的候选人等功能。需要注意的是,候选人编号从1开始,数组下标从0开始,所以在存储选票时要将候选人编号减1。
用c语言写选举统计,假设有六位候选人,有10个人参与投票,用结构体数组统计得票数包含define N 20
好的,我可以帮你解决这个问题。以下是用 C 语言编写的选举统计程序:
```
#include <stdio.h>
#include <stdlib.h>
#define N 20
#define CANDIDATE_NUM 6
struct candidate {
int id;
char name[20];
int votes;
};
int main() {
struct candidate candidates[CANDIDATE_NUM] = {
{1, "Candidate 1", 0},
{2, "Candidate 2", 0},
{3, "Candidate 3", 0},
{4, "Candidate 4", 0},
{5, "Candidate 5", 0},
{6, "Candidate 6", 0}
};
int i, j, vote;
for (i = 0; i < N; i++) {
printf("Please vote for a candidate (input candidate id): ");
scanf("%d", &vote);
for (j = 0; j < CANDIDATE_NUM; j++) {
if (candidates[j].id == vote) {
candidates[j].votes++;
break;
}
}
}
printf("Results:\n");
for (i = 0; i < CANDIDATE_NUM; i++) {
printf("%s: %d votes\n", candidates[i].name, candidates[i].votes);
}
return 0;
}
```
这个程序使用了一个结构体数组来保存候选人的信息,包括候选人的编号、名称和得票数。程序首先初始化了结构体数组,然后循环询问每个选民的投票,并在相应的候选人结构体中增加得票数。最后,程序输出了每个候选人的得票数。
希望这个程序能够解决你的问题,如果还有其他问题,请随时提出。
相关推荐
![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)