| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 553 人关注过本帖
标题:杭电ACM1004题。求大神指出不恰当的地方
只看楼主 加入收藏
赤云
Rank: 2
等 级:论坛游民
帖 子:82
专家分:35
注 册:2014-12-29
结帖率:64.71%
收藏
已结贴  问题点数:20 回复次数:2 
杭电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);
}
搜索更多相关主题的帖子: multiple popular problem secret decide 
2015-04-04 11:03
赤云
Rank: 2
等 级:论坛游民
帖 子:82
专家分:35
注 册:2014-12-29
收藏
得分:0 
能得到想要到结果,但是ACM不通过;
2015-04-04 11:04
beyondyf
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:贵宾
威 望:103
帖 子:3282
专家分:12654
注 册:2008-1-21
收藏
得分:20 
你测试那两个示例能得出正确结果纯属偶然。

1、你动态申请的内存尺寸就不对,谁告诉你那个结构是15个字节的?
2、颜色字符串不超过15个字符,但别忘了还有个字符串的结束标志。
3、申请了内存也不释放。
4、逻辑存在大量冗余。不解释了。

附赠两段代码给你参考(另一段放你另一个同样问题的帖子里了)
程序代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    char s[1024][16];
    int n, m, c, i, j;

    for(; scanf("%d", &n), n; puts(s[m]))
    {
        for(i = 0; i < n; scanf("%s", s[i++]));
        qsort(s, n, sizeof(s[0]), strcmp);
        for(c = i = 0, j = 1; j < n; j++)
            if(strcmp(s[i], s[j]))
            {
                if(c < j - i) c = j - i, m = i;
                i = j;
            }
        if(c < j - i) m = i;
    }
    return 0;
}

重剑无锋,大巧不工
2015-04-05 22:00
快速回复:杭电ACM1004题。求大神指出不恰当的地方
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019849 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved