寒假集训 字符串专题 1001

问题描述:

Problem Description

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.

现有整数N表示输入颜色个数,现需要统计在每组样例中出现次数最多的颜色并输出该颜色。
解题思路:
输入整数N,输入N个单词(用字符串表示每个单词),统计相同单词个数,用数组形式储存每个颜色,然后用标记小标的形式
输出最多个数所代表的颜色。
代码实现:
 1 #include<stdio.h>
 2 #include<string.h>
 3 int main()
 4 { char a[1050][25];
 5  int i,j,N;
 6  while(scanf("%d",&N)!=EOF)
 7  {
 8   getchar();
 9   if(N==0)break;
10   for(i=1;i<=N;i++)
11   gets(a[i]);
12   int s[1050]={0},max=0,k=0;
13   for(i=1;i<=N;i++)
14   {
15    for(j=1;j<=N;j++)
16    {
17     if(strcmp(a[i],a[j])==0)
18     {
19      s[i]++;
20     }
21    }
22   }
23 //  for(i=1;i<=N;i++)
24 //  printf("%d\n",s[i]);
25  
26   for(i=1;i<=N;i++)
27   {
28    if(s[i]>max)
29    {
30     max=s[i];
31     k=i;
32    }
33   }  
34   printf("%s\n",a[k]);
35    
36  }
37  return 0;
38 }
总结:
1.输入字符串时应该注意读入数据时吃掉换行(用getchar())。
2.多实例测试时应该把数据初始化函数memset(数组名,重置数值,sizeof(数组名称))。
3.出错时应该从上往下分块检验,逐步分析结果并改正。

猜你喜欢

转载自www.cnblogs.com/yuanqingwen/p/10376375.html
今日推荐