华为机试-计票统计(HJ94)-纯C
记票统计
https://www.nowcoder.com/practice/3350d379a5d44054b219de7af6708894?tpId=37&&tqId=21317&rp=1&ru=/ta/huawei&qru=/ta/huawei/question-ranking
纯C
这题按要求做就行了,没什么难点。
#include <stdlib.h> #include <stdio.h> #include <string.h> typedef struct Candidate { char * name[20]; int cnt; }Candidate; int main() { int candidates_num = 0, vote_num = 0; while(scanf("%d", &candidates_num) != EOF) { Candidate *candidate = (Candidate *)malloc(sizeof(Candidate) * candidates_num); for(int i=0; i<candidates_num; i++) { scanf("%s", candidate[i].name); candidate[i].cnt = 0; } scanf("%d", &vote_num); char temp[20] = {'\0'}; //存储投票 int invalid = 0; //统计非法投票人数 for(int i=0; i<vote_num; i++) { scanf("%s", temp); int flag = 0; for(int j=0; j<candidates_num; j++) { if(strcmp(temp, candidate[j].name) == 0) { candidate[j].cnt++; flag = 1; } } if(!flag) invalid++; } for(int i=0; i<candidates_num; i++) { printf("%s : %d\n", candidate[i].name, candidate[i].cnt); } printf("Invalid : %d\n", invalid); free(candidate); } return 0; }