杭电ACM1004题。求大神指出不恰当的地方
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);
void f(int n);
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;
do{
scanf("%d",&n);
f(n);
}while(n!=0);//当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;
}
void f(int n){//输出出现次数最多的字符串
int max=0,m=0,n1;
struct A *p,*p1;
char *px;
if(n==0) return ;
p1=p=create(n);
n1=n;
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);
}