HDU1004 (数组元素出现最多)

时间:2022-01-02 18:26:31

HDU1004 思路:求数组中出现次数最多的那个元素: 遍历数组元素,找出每个元素出现的次数

Input
Input contains multiple test cases. Each test case starts with a number N (0 < N <= 1000) -- the total number of balloons distributed. The next N lines contain one color each. The color of a balloon is a string of up to 15 lower-case letters.

A test case with N = 0 terminates the input and this test case is not to be processed.

 
Output
For each case, print the color of balloon for the most popular problem on a single line. It is guaranteed that there is a unique solution for each test case.
 
 HDU1004  (数组元素出现最多)

#include<stdio.h>
#include<string.h>
int main()
{
int i,j,pos,max,n;
int num[1000];
char color[1000][16];
while (scanf("%d",&n)==1&&n)
{
scanf("%s",color[0]);
num[0]=1;
for (i=1;i<n;i++)
{
scanf("%s",color[i]);
num[i]=1;
for (j=0;j<i;j++)
{
if (strcmp(color[i],color[j])==0)
{
num[i]++;
}
}
}
pos=0;
max=0;
for (i=0;i<n;i++)
{
if (num[i]>max)
{
max=num[i];
pos=i;
} }
printf("%s\n",color[pos]);
}
return 0;
}