模拟选票
#include <stdio.h>//包含標(biāo)準(zhǔn)輸入輸出函數(shù)
#include <math.h>
//2019.10.19
//2006-03
//編程模擬選舉過程,假定4位候選人zhang\wang\li\zhao
//代號分別為1、2、3、4
//選舉人直接鍵入候選人代號,1-4之外的為棄權(quán)票、-1為終止標(biāo)志。
//打印各位候選人的得票以及當(dāng)選者的名單、(得票數(shù)超過選票總數(shù)的一半)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為總票數(shù) connt[4]為棄權(quán)票 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){ //有效票=總票-棄權(quán)票 sec=i;}printf("%5s : %d\n",name[i],count[i]);//打印各位候選人的得票}if(sec==-1){printf("沒有超過半數(shù)的人選!"); }else{printf("半數(shù)人選:%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; }
}
?
總結(jié)
- 上一篇: 2021-技能大赛-信息安全管理与评估-
- 下一篇: Playwright 保存图片