求助,请大神看看怎么排序不超时!!
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 编辑 ]