http://acm.hdu.edu.cn/showproblem.php?pid=1004
输入N个字符串 输出出现频率最高的字符串
# include <stdio.h> # include <string.h> # define MAX 1005 struct BALLOON { char Color[20]; int Times;//同颜色气球出现次数 }Balloon[MAX]; int n, Count; void Set_Balloon(char Color[]) { for(int i = 1; i <= Count; i++) { //如果这种颜色已经出现过 次数+1 if(!strcmp(Balloon[i].Color, Color)) { Balloon[i].Times++; return ; } } //否则加入表中 次数==1 strcpy(Balloon[++Count].Color, Color); Balloon[Count].Times = 1; } int main() { while(scanf("%d",&n) && n) { char Temp[20]; Count = 0; //逐个读取颜色并放入表中 for(int i = 1; i <= n; i++) { scanf("%s",Temp); Set_Balloon(Temp); } //遍历表 找到出现频率最高的颜色 int Id; for(int i = 1, max = -1; i <= Count; i++) { if(Balloon[i].Times > max) { max = Balloon[i].Times; Id = i; } } printf("%s\n",Balloon[Id].Color); } return 0; }
时间: 2024-10-14 22:31:53