hd1004哪出错了

wei_xiaohui 2011-11-28 10:38:47
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.



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.



Sample Input
5
green
red
blue
red
red
3
pink
orange
pink
0


Sample Output
red
pink



代码如下:
#include<iostream>
#include<vector>
#include<string>
using namespace std;

int main()
{
//freopen("input.txt","r",stdin);
int N;
while(cin>>N&&N)
{
string str[10010];
int arr[1000];
memset(arr,0,sizeof(arr));
int i,j;
for(i=0;i<N;++i)
{
cin>>str[i];
if(i==0)
arr[i]=1;
else
{
for(j=0;j<i;++j)
if(str[i].compare(str[j])==0)
arr[i]++;
}
}
int k;
for(i=1;i<N;++i)
for(j=0;j<i;++j)
if(arr[i]>arr[j])
{
k=i;
}
cout<<str[k]<<endl;
}
return 0;
}


//**************************************************
#include<iostream>
#include<vector>
#include<string>
using namespace std;

int main()
{
//freopen("input.txt","r",stdin);
int N;
while(cin>>N&&N)
{
string str[10010];
int arr[1000];
memset(arr,0,sizeof(arr));
int i,j;
for(i=0;i<N;++i)
{
cin>>str[i];
for(j=0;j<=i;++j)
{
if(str[i].compare(str[j])==0)
{
arr[j]++;
}
}
}
int k;
for(i=1;i<N;++i)
for(j=0;j<=i;++j)
arr[j]>arr[i]?k=j:k=i;
cout<<str[k]<<endl;
}
return 0;
}
...全文
48 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

64,318

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧