/*
简单题,统计气球数最多的颜色
*/
#include <stdio.h>
#define SIZE_ROW 1001
#define SIZE_COL 16
int main()
{
int N;
int i;
char colors[SIZE_ROW][SIZE_COL];
char color[SIZE_COL];
//存储气球颜色数
int colorNum;
//统计不同颜色气球个数
int balloon[SIZE_ROW];
//用于返回重复颜色气球的下标
int result;
//颜色对多的气球个数
int max;
//颜色最多的气球个数的下标
int maxIndex;
//freopen("F:\\input.txt", "r", stdin);
while ((scanf("%d", &N) == 1) && N != 0)
{
colorNum = 0;
memset(balloon, 0, sizeof(balloon));
memset(colors,0,sizeof(colors));
for (i = 0; i < N; i++)
{
scanf("%s", color);
//搜索颜色数组,如果没有当前输入的颜色,则将该颜色插入颜色数组。否则,对应的气球数+1
result = searchColor(color, colors, colorNum, balloon);
if (result == -1)
{
strcpy(colors[colorNum], color);
balloon[colorNum]++;
colorNum++;
}
else
balloon[result]++;
}
max = balloon[0];
maxIndex = 0;
//寻找气球数最多的颜色
for (i = 1; i < colorNum; i++)
{
if (max < balloon[i])
{
max = balloon[i];
maxIndex = i;
}
}
printf("%s\n", colors[maxIndex]);
}
//freopen("con", "r", stdin);
//system("pause");
return 0;
}
int searchColor(char color[], char colors[][SIZE_COL], int colorNum, int balloon[])
{
int i;
for (i = 0; i < colorNum; i++)
{
if (strcmp(color, colors[i]) == 0)
return i;
}
return -1;
}
原文:https://www.cnblogs.com/mycodinglife/p/10512607.html