菜鸟问题啊!!!!高手帮帮忙啊!!!!

来源:百度知道 编辑:UC知道 时间:2024/06/28 14:59:40
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 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

你的问题挺多的,我就不一一道来了
首先运行上你就没通过,因为你的逻辑就有问题,因为结果只有在输入完毕才会出来,而你一轮输入就会打印一次,当然中间变量肯定也是错的

如1 red 0
或者2 red blue 0

从效率上来说你申请那么大的空间我不知道做什么,事实上你应该动态分配的。

下面是我写的,VC++6.0编译通过(我用了简单的STL,在这下面编译Warining会很多,你用gcc应该会好点)。
#include <iostream>
#include <string>
#include <map>
#include <list>
using namespace std;

map<string,int> countcolor;
list<string> result;
int main()
{
int num=0;
string color="";
while(cin>>num&&num)
{
if(num<0||num>1000)
{
cout<<"overflow,please input again."<<endl;
continue;
}
int maxcount=0;string maxcolor="";
for(int i=0;i<num;i++)
{
cin>>color;
if(countcolor.find(color)!=countcolor.end())
{
countcolor[color]++;
}
else