杭电1004大家来帮看下哪错了吧谢了~~~
题目Inputcontainsmultipletestcases.EachtestcasestartswithanumberN(0<N<=1000)--thetotaln...
题目
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()
{
char temp[15],buf[1000][15];
int count[1000]={0},i,j,k,n,l,max=1;
scanf("%d",&n);
while(n!=0)
{
for(i=0;i<n;i++)
{
scanf("%s",temp);
for(l=0,j=1;l<j;l++)
{
k=strcmp(temp,buf[l]);
if(k==0)
{
count[l]++;
break;
}
if(l==j-1)
{
strcpy(buf[j],temp);
j++;
break;
}
}
}
for(i=1;i<j;i++)
{ if(count[max]<count[i])
max=i;
}
puts(buf[max]);
scanf("%d",&n);
}
return 0;
} 展开
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()
{
char temp[15],buf[1000][15];
int count[1000]={0},i,j,k,n,l,max=1;
scanf("%d",&n);
while(n!=0)
{
for(i=0;i<n;i++)
{
scanf("%s",temp);
for(l=0,j=1;l<j;l++)
{
k=strcmp(temp,buf[l]);
if(k==0)
{
count[l]++;
break;
}
if(l==j-1)
{
strcpy(buf[j],temp);
j++;
break;
}
}
}
for(i=1;i<j;i++)
{ if(count[max]<count[i])
max=i;
}
puts(buf[max]);
scanf("%d",&n);
}
return 0;
} 展开
1个回答
展开全部
你没有考虑输入数值很多的情况,就是说测试数据不止一组,我发我的ac代码上来你看看吧,你要提高acm的话就必须根据别人的ac代码来找出自己的错误,这样才能提高的,加油#include <stdio.h>
#include<string.h>
int main()
{
int n,i,j,a[1001],k,flag=0,m,p;char s[1001][16],d[1001][16];
while(scanf("%d",&n)!=EOF&&n!=0)
{
k=0;getchar();
for(i=0;i<n;i++)
{
a[i]=0;
scanf("%s",s[i]);
if(i==0)
{
strcpy(d[k],s[i]);
a[k]++;
}
if(i!=0)
for(j=0;j<=k;j++)
{
if(strcmp(s[i],d[j])==0)
{
a[j]++;
break;
}
else
{
strcpy(d[k+1],s[i]);
a[k+1]++;
flag=1;
}
}
if(flag==1)
{
k++;
flag=0;
}
}
m=0;
for(i=0;i<k;i++)
{
if(a[i]>m)
{
m=a[i];
p=i;
}
}
printf("%s\n",s[p]);
}
return 0;
}
#include<string.h>
int main()
{
int n,i,j,a[1001],k,flag=0,m,p;char s[1001][16],d[1001][16];
while(scanf("%d",&n)!=EOF&&n!=0)
{
k=0;getchar();
for(i=0;i<n;i++)
{
a[i]=0;
scanf("%s",s[i]);
if(i==0)
{
strcpy(d[k],s[i]);
a[k]++;
}
if(i!=0)
for(j=0;j<=k;j++)
{
if(strcmp(s[i],d[j])==0)
{
a[j]++;
break;
}
else
{
strcpy(d[k+1],s[i]);
a[k+1]++;
flag=1;
}
}
if(flag==1)
{
k++;
flag=0;
}
}
m=0;
for(i=0;i<k;i++)
{
if(a[i]>m)
{
m=a[i];
p=i;
}
}
printf("%s\n",s[p]);
}
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询