纯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; }