要是候选人票数一样呢
程序代码:
#include<stdio.h> #include"stdlib.h" #include"assert.h" #include"string.h" typedef struct _PERSON { char *name; int count; }PERSON; int main() { printf("请输入有几位候选人:"); int n; scanf("%d", &n); PERSON *p = (PERSON *)malloc(sizeof(PERSON)*n); assert(p != NULL); for (int j = 0; j < n; j++) { char *s = (char *)malloc(sizeof(char) * 30); assert(s != NULL); p[j].name = s; printf("请输入有第%d候选人名字:", j + 1); scanf("%s", s); p[j].count=0; //count赋初始值 } char input[20]; printf("请输入有几个选举人:"); int m; scanf("%d", &m); fflush(stdin); //清楚之前的数据 for (int i = 0; i < m; i++) { printf("第%d投票人:",i+1); gets(input); for (int k = 0; k < n; k++) { if (strcmp(p[k].name, input) == 0) { p[k].count++; break; } } } PERSON max=p[0]; for (int a = 0; a < n - 1; a++) { if (p[a].count > p[a + 1].count) { max = p[a]; } } printf("票数最多的是:"); printf("%s-%d", max.name,max.count); //多了一个max,已经去掉 return 0; }
[此贴子已经被作者于2017-2-11 22:28编辑过]
早知做人那么辛苦! 当初不应该下凡