[恢]hdu 1004

2011-12-16 08:14:58

地址:http://acm.hdu.edu.cn/showproblem.php?pid=1004

题意:给出n个字符串,输出出现的最多的那个。

代码:

# include <stdio.h>
# include <string.h>


typedef struct node{
char name[20] ;
int num ;
}node ;


node a[1010] ;
int cnt ;


void add(char name[])
{
int i ;
for (i = 0 ; i < cnt ; i++)
{
if (0 == strcmp(a[i].name, name))
{
a[i].num++ ;
return ;
}
}
strcpy (a[cnt].name, name) ;
a[cnt].num = 1 ;
cnt++ ;
}


int main ()
{
int n, i, pos ;
char name[20] ;

while (scanf ("%d%*c", &n) && n)
{
cnt = 0 ;
for (i = 0 ; i < n ; i++)
{
gets(name) ;
add(name) ;
}
pos = 0 ;
for (i = 1 ; i < n ; i++)
if (a[i].num > a[pos].num) pos = i ;
printf ("%s\n", a[pos].name) ;
}
}



原文地址:https://www.cnblogs.com/lzsz1212/p/2314965.html