| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 850 人关注过本帖
标题:浙大OJLunch Time 出现Segmentation Fault求大神解
只看楼主 加入收藏
末日泡沫
Rank: 1
等 级:新手上路
帖 子:18
专家分:0
注 册:2015-3-1
结帖率:100%
收藏
 问题点数:0 回复次数:6 
浙大OJLunch Time 出现Segmentation Fault求大神解
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int cmp(const void *x,const void *y)
{
    return *(int *)y-*(int *)x;
}
int main()
{
    int t,s,m,n,d,i,j,k,l,sum,max,q;
    char str[110][110];
    char str1[110][110];
    char str2[110][110];
    int a[110];
    int b[110];
    int c[110];
    int h[110];
    int e[110];
    int f[110];
    int g[110];
    scanf("%d",&t);
    while(t--)
    {
        sum=0;
        max=-1;
        scanf("%d%d%d",&s,&n,&d);
        for(i=0;i<s;i++)
        scanf("%s %d",str[i],&a[i]);
        for(i=0;i<n;i++)
        scanf("%s %d",str1[i],&b[i]);
        for(i=0;i<d;i++)
        scanf("%s %d",str2[i],&c[i]);
        for(i=0;i<s;i++)
        e[i]=a[i];
        for(i=0;i<n;i++)
        f[i]=b[i];
        for(i=0;i<d;i++)
        g[i]=c[i];
        if(s%2==0)
        {
            for(i=0;i<s;i++)
            {
                if(a[i]>max)
                {
                    max=a[i];
                    l=i;
                }
            }
            h[0]=l;
        }
        if(s%2==1)
        {
            qsort(e,s,sizeof(e[0]),cmp);
            q=e[1];
            for(i=0;i<s;i++)
            {
                if(a[i]==q)
                {
                    l=i;
                    break;
                }
            }
            h[0]=l;
        }
        max=-1;
        if(n%2==0)
        {
            for(i=0;i<n;i++)
            {
                if(b[i]>max)
                {
                    max=b[i];
                    l=i;
                }
            }
            h[1]=l;
        }
        if(n%2==1)
        {
            qsort(f,n,sizeof(f[0]),cmp);
            q=f[1];
            for(i=0;i<n;i++)
            {
                if(b[i]==q)
                {
                    l=i;
                    break;
                }
            }
            h[1]=l;
        }
        max=-1;
        if(d%2==0)
        {
            for(i=0;i<d;i++)
            {
                if(c[i]>max)
                {
                    max=c[i];
                    l=i;
                }
            }
            h[2]=l;
        }
        if(d%2==1)
        {
            qsort(g,d,sizeof(g[0]),cmp);
            q=g[1];
            for(i=0;i<d;i++)
            {
                if(c[i]==q)
                {
                    l=i;
                    break;
                }
            }
            h[2]=l;
        }
        printf("%d %s %s %s\n",a[h[0]]+b[h[1]]+c[h[2]],str[h[0]],str1[h[1]],str2[h[2]]);
    }
    return 0;
}
虽然有点啰嗦 但是感觉思路是对的  输出也没错
搜索更多相关主题的帖子: include return 浙大 
2015-07-17 11:32
末日泡沫
Rank: 1
等 级:新手上路
帖 子:18
专家分:0
注 册:2015-3-1
收藏
得分:0 
这一题我已经AC了 问题应该是输入的问题
2015-07-17 14:10
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9032
专家分:54066
注 册:2011-1-18
收藏
得分:0 
最起码要贴个题目要求吧
2015-07-17 15:07
末日泡沫
Rank: 1
等 级:新手上路
帖 子:18
专家分:0
注 册:2015-3-1
收藏
得分:0 
Lunch Time
Time Limit: 2 Seconds      Memory Limit: 65536 KB
The 999th Zhejiang Provincial Collegiate Programming Contest will be held in Marjar University. The canteen of Marjar University is making preparations for this grand competition. The canteen provides a lunch set of three types: appetizer, main course and dessert. Each type has several dishes with different prices for choosing.

Edward is the headmaster of Marjar University. One day, to inspect the quality of dishes, he go to the canteen and decides to choose a median set for his lunch. That means he must choose one dish from each of appetizers, main courses and desserts. Each chosen dish should at the median price among all dishes of the same type.

For example, if there are five dessert dishes selling at the price of 2, 3, 5, 10, 30, Edward should choose the dish with price 5 as his dessert since its price is located at the median place of the dessert type. If the number of dishes of a type is even, Edward will choose the dish which is more expensive among the two medians.

You are given the list of all dishes, please write a program to help Edward decide which dishes he should choose.

Input

There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:

The first line contains three integers S, M and D (1 <= S, M, D <= 100), which means that there are S dishes of appetizer, M dishes of main course and D dishes of dessert.

Then followed by three parts. The first part contains S lines, the second and the last part contains M and D lines respectively. In each line of the three parts, there is a string and an integer indicating the name and the price of a dish. The name of dishes will only consist of non-whitespace characters with no more than 50 characters. The price of dishes are non-negative integers less than or equal to 1000. All dish names will be distinct.

Output

For each test case, output the total price of the median set, together with the names of appetizer, main course and dessert, separated by a single space.

Sample Input

2
1 3 2
Fresh_Cucumber 4
Chow_Mein 5
Rice_Served_with_Duck_Leg 12
Fried_Vermicelli 7
Steamed_Dumpling 3
Steamed_Stuffed_Bun 4
2 3 1
Stir-fried_Loofah_with_Dried_Bamboo_Shoot 33
West_Lake_Water_Shield_Soup 36
DongPo's_Braised_Pork 54
West_Lake_Fish_in_Vinegar 48
Longjing_Shrimp 188
DongPo's_Crisp 18
Sample Output

15 Fresh_Cucumber Fried_Vermicelli Steamed_Stuffed_Bun
108 West_Lake_Water_Shield_Soup DongPo's_Braised_Pork DongPo's_Crisp
2015-07-17 20:16
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9032
专家分:54066
注 册:2011-1-18
收藏
得分:0 
我看了一下你的代码,……,不评价,给你一个演示吧,不保证无误
程序代码:
#include <stdio.h>

struct dish
{
    char name[51]; // "no more than 50 characters"
    unsigned price; // "non-negative integers less than or equal to 1000"
};

size_t median( const struct dish[], size_t n );

int main( void )
{
    unsigned t;
    scanf( "%u", &t );
    while( t-- )
    {
        unsigned s, m, d; // "1 <= S, M, D <= 100"
        struct dish buf[100+100+100];

        scanf( "%u%u%u", &s, &m, &d );
        for( size_t i=0; i!=s+m+d; ++i )
            scanf( "%s%u", buf[i].name, &buf[i].price );

        size_t idx_s = median( buf, s );
        size_t idx_m = median( buf+s, m ) + s;
        size_t idx_d = median( buf+s+m, d ) + s + m;

        printf( "%u %s %s %s\n", buf[idx_s].price+buf[idx_m].price+buf[idx_d].price, buf[idx_s].name, buf[idx_m].name, buf[idx_d].name );
    }

    return 0;
}

size_t median( const struct dish ds[], size_t n )
{
    size_t sort[1000+1] = { 0 }; // "The price of dishes are non-negative integers less than or equal to 1000" & "different prices"
    for( size_t i=0; i!=n; ++i )
        sort[ ds[i].price ] = i+1;

    size_t idx = 0;
    for( size_t cnt=0; cnt!=n/2+1; ++idx )
        cnt += (sort[idx]!=0);

    return sort[idx-1]-1;
}

2015-07-20 11:00
末日泡沫
Rank: 1
等 级:新手上路
帖 子:18
专家分:0
注 册:2015-3-1
收藏
得分:0 
回复 5楼 rjsp
谢谢 领教了
2015-07-21 08:53
自傲
Rank: 2
等 级:论坛游民
帖 子:53
专家分:95
注 册:2015-5-16
收藏
得分:0 
2015-07-21 10:09
快速回复:浙大OJLunch Time 出现Segmentation Fault求大神解
数据加载中...
 
   



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

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