| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 446 人关注过本帖
标题:杭电1004为什么不过?
只看楼主 加入收藏
zbjzbj
Rank: 12Rank: 12Rank: 12
来 自:郑州
等 级:贵宾
威 望:52
帖 子:633
专家分:3045
注 册:2011-4-22
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:8 
杭电1004为什么不过?
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 <string.h>
void find(char* str);
int max();
struct Color
{
    int num;
    char color[16];
} book[20];

int main()
{
    int i = 0;
    int j = 0;
    char balloon[20][1000][16] = {0};      //输入数据
    while (scanf("%d", &i) && i != 0)
    {
        for (int p = 0; p < i; p++)
        {
            scanf("%s", balloon[j][p]);
        }
        j++;
    }

    i = 0;
    while (balloon[i][0][0])               //输出
    {
        for (int p = 0; p < 20; p++)
        {
            book[p].num = 0;
            book[p].color[0] = 0;
        }
        j = 0;
        while (balloon[i][j][0])
        {
            find(balloon[i][j]);
            j++;
        }
        max();
        i++;
    }
    return 0;
}

void find(char* str)  // str[])                 //统计气球每种颜色数量
{
    int k = 0;
    while (book[k].num )
    {
        if (strcmp(str, book[k].color))
        {
            k++;
        }
        else
        {
            book[k].num++;
            return;
        }
    }
    strcpy(book[k].color, str);
    book[k].num = 1;
}

int max()                                 //输出数量最多的气球颜色
{
    int m = 0;
    int inMax = 0;
    while (book[m].num != 0)
    {
        if (book[inMax].num < book[m].num)
        {
            inMax = m;
        }
        m++;
    }
    printf("%s\n", book[inMax].color);
    return 0;
}



[此贴子已经被作者于2023-8-11 09:32编辑过]

搜索更多相关主题的帖子: color str int the num 
2023-08-11 09:29
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:20 
题目完整吗?我看你的代码,第一个数字是20,但这个20是哪里来的,题目中没有呀?
2023-08-11 09:55
zbjzbj
Rank: 12Rank: 12Rank: 12
来 自:郑州
等 级:贵宾
威 望:52
帖 子:633
专家分:3045
注 册:2011-4-22
收藏
得分:0 
回复 2楼 rjsp
题目文字完整复制,没有进行截图
题目描述没有说输入多少组气球,所以编程时为了方便预设输入20组气球,用一个三维数组储存全部输入,并且假定气球的颜色少于20种。
是学生练编程,可能代码质量不是太高。

提交以后返回:错误回答

[此贴子已经被作者于2023-8-11 10:21编辑过]

2023-08-11 10:18
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:0 
题目描述没有说输入多少组气球,所以编程时为了方便预设输入20组气球,用一个三维数组储存全部输入,并且假定气球的颜色少于20种。
这也能瞎假设?

你的代码中变量交杂,如乱麻,看得废眼睛。我重新写一个,没测试,仅供参考
程序代码:
#include <stdio.h>
#include <string.h>

int main( void )
{
    for( unsigned n; scanf("%u",&n)==1 && n!=0; )
    {
        struct color_num {
            char color[16];
            unsigned count;
        } cns[1000];
        unsigned cns_size = 0;

        for( unsigned i=0; i!=n; ++i )
        {
            char color[16];
            scanf( "%s", color );

            unsigned index;
            for( index=0; index!=cns_size && strcmp(color,cns[index].color)!=0; ++index );

            if( index == cns_size )
            {
                ++cns_size;
                strcpy( cns[index].color, color );
                cns[index].count = 1;
            }
            else
                ++cns[index].count;
        }

        struct color_num* pmax = &cns[0];
        for( unsigned i=1; i!=cns_size; ++i )
        {
            if( pmax->count < cns[i].count )
                pmax = &cns[i];
        }
        puts( pmax->color );
    }
}
2023-08-11 10:25
zbjzbj
Rank: 12Rank: 12Rank: 12
来 自:郑州
等 级:贵宾
威 望:52
帖 子:633
专家分:3045
注 册:2011-4-22
收藏
得分:0 
这个不符合样例,各组数据输入完成以后才做输出。
Sample Input
5
green
red
blue
red
red
3
pink
orange
pink
0
 

Sample Output
red
pink
2023-08-11 10:48
zbjzbj
Rank: 12Rank: 12Rank: 12
来 自:郑州
等 级:贵宾
威 望:52
帖 子:633
专家分:3045
注 册:2011-4-22
收藏
得分:0 
好吧,有思路了。
把储存输入变成储存结果,集中输出。
2023-08-11 11:15
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:0 
回复 5楼 zbjzbj
符合要求的。
题目只要求输入是什么、输出是什么,并没有要求 输入输出合起来是什么。

我怀疑你刚接触电脑,尚不理解。
输入是从stdin取数据,输出是向stdout存数据,console控制台只是把stdin和stdout的内容混在了一起而已。
2023-08-11 11:27
zbjzbj
Rank: 12Rank: 12Rank: 12
来 自:郑州
等 级:贵宾
威 望:52
帖 子:633
专家分:3045
注 册:2011-4-22
收藏
得分:0 
一楼的代码book[20] —> book[100]就过了,色彩种类预设的少了。
四楼的代码也过
2023-08-11 18:14
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:0 
回复 8楼 zbjzbj
除了你这种,比赛中还出现过的真实案例有: 某次比赛中,有考生黑进判定系统,把判定用的输出文件改掉了。

常见的方法还可以贿赂、代考、假证书,……
2023-08-12 09:16
快速回复:杭电1004为什么不过?
数据加载中...
 
   



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

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