| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 759 人关注过本帖
标题:求助,请大神看看怎么排序不超时!!
只看楼主 加入收藏
caixin_oo1
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2014-11-28
结帖率:0
收藏
已结贴  问题点数:20 回复次数:2 
求助,请大神看看怎么排序不超时!!
PAT BASIC 1015,德才论,段错误 ,求助
题目网站http://www.
写得特别烂,要喷从轻
Q1:不知道怎么一个排序语句满足题目要求所以写了两个
Q2:答案错误的地方是哪呢(从内存看)

测试点    结果    用时(ms)    内存(kB)    得分/满分
0    答案正确    1    264    12/12
1    答案正确    1    256    2/2
2    答案错误    92    540    0/3
3    运行超时            0/3
4    运行超时            0/3
5    答案正确    1    264    2/2
程序代码:
#include<stdio.h>
#include<string.h>

struct student 
{
    long long num;
    int mor;
    int wisd;
};

int main()
{
    struct student stu[5][100001];
    int i=1,count[5]={0,1,1,1,1},N,pass,k,pre,stuMor,stuWisd;
    long long num;
    int aStu(int d,int c,int pre);
    int bStu(int d,int c,int pre);
    int cStu(int d,int c,int pre);
    
    void sort(struct student stu[][100001],int k,int n);
    
    scanf("%d%d%d",&N,&pass,&pre);
    
    while(i <= N)
    {
        scanf("%lld %d %d",&num,&stuMor,&stuWisd);
        if(stuMor >= pass && stuWisd >= pass)
        {
            if (aStu(stuMor,stuWisd,pre))
            {
                stu[1][count[1]].num=num;
              stu[1][count[1]].mor = stuMor;
              stu[1][count[1]].wisd = stuWisd;
              count[1]++;
              count[0]++;
            }
            else if (bStu(stuMor,stuWisd,pre))
            {
                stu[2][count[2]].num=num;
              stu[2][count[2]].mor = stuMor;
              stu[2][count[2]].wisd = stuWisd;
              count[2]++;
              count[0]++;
            }
            else if (cStu(stuMor,stuWisd,pre))
            {
                stu[3][count[3]].num=num;
              stu[3][count[3]].mor = stuMor;
              stu[3][count[3]].wisd = stuWisd;
              count[3]++;
              count[0]++;
            }
            else 
            {
              stu[4][count[4]].num=num;
              stu[4][count[4]].mor = stuMor;
              stu[4][count[4]].wisd = stuWisd;
              count[4]++;
              count[0]++;
            }
        }
        i++; 
    }
    
    printf("%d\n",count[0]);
    
    for(k = 1;k <= 4;k++)
    {
        sort(stu,k,count[k]-1);
    }
    
    for(i = 1;i <= 4;i++)
    {
        for(k = 1;k <= count[i]-1;k++)
        {
            printf("%lld %d %d\n",stu[i][k].num,stu[i][k].mor,stu[i][k].wisd);
        }
    }
}

int aStu(int d,int c,int pre)
{
    return d >= pre && c >= pre;
}
int bStu(int d,int c,int pre)
{
    return d >= pre && c < pre;
}
int cStu(int d,int c,int pre)
{
    return d < pre && c < pre && d > c;
}


void sort(struct student stu[][100001],int k,int n)
{
    int i,j,key,mini;
    struct student Key;
    

    for(j = 1;j < n;j++)
    {
        mini = 1;
        for(i = 1;i <= n-j+1;i++)
        {
            if(stu[k][mini].mor + stu[k][mini].wisd == stu[k][i].mor + stu[k][i].wisd)
            {
                if(stu[k][mini].mor > stu[k][i].mor) 
                {
                    mini = i;
                    //printf("0\n");
                }
                else if(stu[k][mini].mor == stu[k][i].mor)
                {
                  if (stu[k][mini].num < stu[k][i].num)
                    {
                        mini = i;
                        //printf("1\n");
                    }
                }
            }
        }
          Key = stu[k][mini];
            stu[k][mini] = stu[k][i-1];
            stu[k][i-1] = Key;
            //for(key = 1; key <= n;key++) printf("%lld %d %d\n",stu[k][key].num,stu[k][key].mor,stu[k][key].wisd);
    }
            
    for(j = 2;j <= n;j++)
    {
        i = j - 1;
        key = stu[k][j].mor + stu[k][j].wisd;
        Key = stu[k][j];
        while (i > 0 && stu[k][i].mor + stu[k][i].wisd < key)
        {
            stu[k][i+1]=stu[k][i];
            i--;
        }
        stu[k][i+1] = Key;
    }
}


[ 本帖最后由 caixin_oo1 于 2014-12-1 22:54 编辑 ]
搜索更多相关主题的帖子: include color 网站 
2014-12-01 19:31
caixin_oo1
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2014-11-28
收藏
得分:0 
段错误是数组内存溢出???
2014-12-01 22:41
pauljames
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:千里冰封
威 望:9
帖 子:1555
专家分:10000
注 册:2011-5-8
收藏
得分:20 
试试看快速排序算法?

经常不在线不能及时回复短消息,如有c/单片机/运动控制/数据采集等方面的项目难题可加qq1921826084。
2014-12-07 21:55
快速回复:求助,请大神看看怎么排序不超时!!
数据加载中...
 
   



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

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