(ACM题)结果正确,但反馈wrong answer。怎么回事?
请大神指教,拜谢!试题:
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"malloc.h"
int fun(char *p1,char *p2);
struct A{
char a[15];
struct A *next;
};
struct A *create(int n){
struct A *p,*tail,*head;
int i;
if(n==1){p=(struct A*)malloc(15);
scanf("%s",p->a);
p->next=NULL;
return p;
}
else for(i=1;i<=n;i++){
p=(struct A*)malloc(15);
scanf("%s",p->a);
if(i==1){head=p; tail=p;}
else {tail->next=p; tail=p; p->next=NULL;}
}
return head;
}
int main(){
int n,max=0,m=0,n1;
struct A *p,*p1;
char *px;
do{
scanf("%d",&n);
if(n==0) break;
p1=p=create(n);
n1=n;
px=p->a;
do{
while(n--){
if(fun(p->a,p1->a)) m++;
if(p->next!=NULL) p=p->next;
else {p=p->next;break;}
}
n=n1; if(m>max) {px=p1->a;max=m;}m=0; p=p1; p1=p1->next;
}while(p1->next!=NULL);
printf("\n");
puts(px);
}while(n!=0);
return 0;
}
int fun(char *p1,char *p2){
while(*p1==*p2){
if(*p1=='\0')break;
p1++;
p2++;
}
if(*p1=='\0') return 1;
else return 0;
}