杭电acm 1004题

本题的大意是要求计算出颜色出现频率最高的那个气球...

虽然不是很难,但是还是学到了一些知识,二维数组的运用,由于题目输入的是不超过15个字符的颜色,所以我使用一个二维数组来装颜色,每一行装一种颜色...再与以前初次的颜色进行比较,得到最大的那个数据即使出现次数最高的那个颜色,输出即可...代码如下:

 1 /********************************************************
 2 杭电acm 1004题 已AC
 3 *********************************************************/
 4 #include <iostream>
 5 using namespace std;
 6 #define Max 1000
 7 int main(void)
 8 {
 9     int N;
10     int temp;
11     int mark=0;
12     char ballon[1000][30];//二维数组来装填颜色
13     int result[1000]={0};
14     cin>>N;
15     while(N)
16     {
17         for(int i=0;i<N;i++)
18         {
19             cin>>ballon[i];//每一行装一种颜色
20             if(i==0)
21             {result[0]=1;continue;}
22             
23             else 
24             {
25                 result[i]=1;
26                 for(int j=0;j<i;j++)
27                 {
28                     if(!strcmp(ballon[i],ballon[j]))//与以前的颜色进行对比...出现则加1
29                     {
30                         result[j]+=1;
31                     }
32                 }
33             }
34         }
35         temp=result[0];
36         mark=0;
37         for(i=0;i<N;i++)
38         {    
39             if(result[i]>temp)
40             {
41                 temp=result[i];
42                 mark=i;//得到最大的那个颜色的下标,与颜色对应起来
43             }
44                 
45         }
46         cout<<ballon[mark]<<endl;
47         cin>>N;
48     }
49     return 0;
50 }

补充一点知识,strcmp(const char *str1,const char *str2),其比较结果如果是两个字符串相等则返回0,如果str1<str2则返回负数,如果str1>str2则返回正数

原文地址:https://www.cnblogs.com/kb342/p/3691438.html