Let the Balloon Rise
Problem Description
Contest time again! How excited it is to see balloons floating around. But to tell you a secret, the judges' favorite time is guessing the most popular problem. When the contest is over, they will count the balloons of each color and find the result.
This year, they decide to leave this lovely job to you.
This year, they decide to leave this lovely job to you.
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.
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.
Sample Input
5 green red blue red red 3 pink orange pink 0
Sample Output
然后将对应的出现次数付成1,进行相互比较,并将相同的两个元素的出现次数都加上一,之后
扫描整个time即可找到最多的次数。(个人认为:因为还没有学习,vector的用处是将字符串压进其中
方便进行比对
代码:
#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main()
{
int num;
cin>>num;
while(num!=0)
{
string str;
vector<int> time;
vector<string> vecs;
for(int i=0;i<num;i++)
{
cin>>str;
vecs.push_back(str);
}
for(int j=0;j<vecs.size();j++)
{
time.push_back(1);
}
for(int i=0;i<vecs.size();i++)
for(int j=i+1;j<vecs.size();j++)
{
if(vecs[i]==vecs[j])
{
time[i]++;
time[j]++;
}
}
int max=0;
string strmost;
for(int i=0;i<time.size();i++)
{
if(time[i]>max)
{
max = time[i];
strmost=vecs[i];
}
}
cout<<strmost<<endl;
cin>>num;
}
return 0;
}
red pink思路:建立一个vector的time 和 vecs方便向其中添加数据,输入str将每个str压入vector地步
然后将对应的出现次数付成1,进行相互比较,并将相同的两个元素的出现次数都加上一,之后
扫描整个time即可找到最多的次数。(个人认为:因为还没有学习,vector的用处是将字符串压进其中
方便进行比对
代码:
#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main()
{
int num;
cin>>num;
while(num!=0)
{
string str;
vector<int> time;
vector<string> vecs;
for(int i=0;i<num;i++)
{
cin>>str;
vecs.push_back(str);
}
for(int j=0;j<vecs.size();j++)
{
time.push_back(1);
}
for(int i=0;i<vecs.size();i++)
for(int j=i+1;j<vecs.size();j++)
{
if(vecs[i]==vecs[j])
{
time[i]++;
time[j]++;
}
}
int max=0;
string strmost;
for(int i=0;i<time.size();i++)
{
if(time[i]>max)
{
max = time[i];
strmost=vecs[i];
}
}
cout<<strmost<<endl;
cin>>num;
}
return 0;
}