hdu_1002_Let the Balloon Rise (模拟)

2015-01-24 05:38:53 · 作者: · 浏览: 3

?

题意:输入n个气球的颜色,(气球的颜色是一个字符串,最多有15个小写字母),输出最多相同颜色气球的颜色,输入0结束程序。

保证每组数据都有唯一且具有最多的一种颜色的气球。

解题思路:利用string类型的数组保存n个气球的颜色

样例:

1)a b a d e

2)a b a d e

a->a b a d e

b->a b a d e

c->a b a d e

d->a b a d e

1)中的每个元素与2)中的每个元素比较,相等则cnt++,且最终把最大的元素个数保存在word中。

记录最大元素个数的下标,保存在flag中。

?

#include 
   
     #include 
    
      using namespace std; int main(int argc, char *argv[]) { int n; string color[1010];//string类型的数组 while(1) { string color[1010] = {};//初始化为空的数组,没有这个初始化会WA cin >> n; if(n == 0) return 0; for(int i = 0;i < n;i++) { cin >> color[i]; } int cnt,flag = 10000; int word = 0; for(int i = 0;i < n;i++) { cnt = 0; for(int j = 0;j < n;j++) { if(color[i] == color[j]) { cnt++;//元素相等则++ } } if(cnt > word) { word = cnt;//最大的元素个数保存在word中 flag = i;//记录最大元素个数的下标 } } cout << color[flag] << endl; } return 0; }
    
   


?