#include <stdio.h>//包含标准输入输出函数
#include <math.h>
//2019.10.19
//2006-03
//编程模拟选举过程,假定4位候选人zhang\wang\li\zhao
//代号分别为1、2、3、4
//选举人直接键入候选人代号,1-4之外的为弃权票、-1为终止标志。
//打印各位候选人的得票以及当选者的名单、(得票数超过选票总数的一半)
int main(){
void Count(char ch,int count[5]);
int i;
char ch;
char name[4][10]={"zhang","wang","li","zhao"};
int count[5]={0},num=0,sec=-1; //num为总票数 connt[4]为弃权票
while(1){
ch=getchar();
if(ch=='-'){
ch=getchar();
if(ch=='1')
break;
}
num++;
Count(ch,count);
}
for(i=0;i<4;i++){
if(count[i]>(num-count[4])/2){ //有效票=总票-弃权票
sec=i;
}
printf("%5s : %d\n",name[i],count[i]);//打印各位候选人的得票
}
if(sec==-1){
printf("没有超过半数的人选!");
}else{
printf("半数人选:%5s",name[sec]);
}
return 0;
}
void Count(char ch,int count[5]){
switch(ch){
case '1':count[0]++;break;
case '2':count[1]++;break;
case '3':count[2]++;break;
case '4':count[3]++;break;
default: count[4]++;break;
}
}
模拟选票
猜你喜欢
转载自blog.csdn.net/yky__xukai/article/details/102644199
今日推荐
周排行