题解 | #记票统计#
记票统计
http://www.nowcoder.com/practice/3350d379a5d44054b219de7af6708894
用scanf函数处理输入,投票的时候每投入一张票,和候选人匹配,统计结果,这样的好处是可以无视投票者的数量,只需要给候选人分配空间。
#include "stdio.h" #include "string.h" char in_put[100][30] = {0}; char out_put[30] = {0}; int out[100] = {0}; int main(void) { int i, j, m = 0, n = 0, invalid = 0; scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%s", in_put[i]); } scanf("%d", &m); for (i = 0; i < m; i++) { scanf("%s", out_put); for (j = 0; j < n; j++) { if (strcmp(out_put, in_put[j]) == 0) { out[j] ++; break; } } if (j == n) { invalid++; } } for (i = 0; i < n; i++) { printf("%s : %d\n", in_put[i], out[i]); } printf("Invalid : %d", invalid); return 0; }