问题描述
- 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
0Sample 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("%sn",a[r]);}
t=n;
}
return 0;
}
解决方案
#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;
}
}
解决方案二:
#include
using namespace std;
#include
#include
int main()
{
int N,i,j,m=0,flag=0,max,res;
char str[16],s[100][16],a[100];
while(scanf("%d",&N)!=EOF&&N!=0)
{
getchar();
m=0;max=0;res=0;
memset(a,0,sizeof(a));
for(i=1;i<=N;i++)
{
gets(str);flag=0;
for(j=0;j
{
if(strcmp(s[j],str)==0)
{flag=1;
a[j]++;
break;}
}
if(!flag)
{
strcpy(s[m],str);
a[m]++;
m++;
}
}
for(j=0;j
{if(a[j]>max){max=a[j];
res=j;}}
cout<<s[res]<<endl;
}
}