ajsdfkjasdfasdif 2015-12-03 10:58 采纳率: 16.7%
浏览 1613

HDOJ1004 用C语言做的不知道哪错了帮忙看一下问题出在哪里

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.

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"stdio.h"
#include"string.h"
int main()
{
int t=1;
while(t)
{
int n,j,i,b[1000];
char a[1000][18];
scanf("%d",&n);
if(n20)
return 0;
for(i=0;i {
scanf("%s",&a[i]);
if(strlen(a[i])>15)
return 0;
}
for(i=0;i for(j=i+1;j if(strcmp(a[i],a[j])>0)
{char t[1000];strcpy(t,a[i]);strcpy(a[i],a[j]);strcpy(a[j],t);}
char s[1000];strcpy(s,a[0]);int ka=0;
for(i=0;i if(strcmp(a[i],s)==0)
{ka++,b[i]=ka;}
else
{b[i]=1;strcpy(s,a[i]);ka=1;}
int max=b[0];
for(i=0;i if(b[i]>max)
max=b[i];
int r;
for(i=0;i<n;i++)
if(b[i]==max)
{r=i;printf("%s\n",a[r]);}
t=n;
}
return 0;
}

  • 写回答

2条回答 默认 最新

  • 远方的飞猪 2015-12-03 13:45
    关注

    #include
    #include
    using namespace std;
    int main()
    {
    int n,k;
    string a,b;
    while(cin>>n)
    {
    k=0;//必须在while中重新定义k
    mapcolor;//必须重新清空或定义color
    if(n==0)
    break;
    for(int i=0;i {
    cin>>a;
    if(!color.count(a))
    {color[a]=1;}
    else
    {color[a]++;}
    k=k>color[a]?k:color[a];
    if(k==color[a]) //当color[a] 达到最大值
    {b=a;}
    }
    cout<<b<<endl;
    }
    }

    评论

报告相同问题?

悬赏问题

  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?