注意事项:
题目没说清楚。先按成绩升序排,成绩相同则按名字字母序升序排,再相同的话按年龄升序排。
参考代码:
#include<stdio.h> #include<string.h> #include<stdlib.h> typedef struct Stu{ char name[101]; int age; int score; } Stu; int cmp(const void *a, const void *b){ Stu *c = (Stu *)a; Stu *d = (Stu *)b; if(c->score != d->score) return c->score - d->score; if(strcmp(c->name, d->name)) return strcmp(c->name, d->name); return c->age - d->age; } int main() { Stu stus[1000]; int i, n; while(scanf("%d", &n) != EOF){ getchar(); for(i = 0; i < n; i++){ scanf("%s%d%d", stus[i].name, &stus[i].age, &stus[i].score); getchar(); } qsort(stus, n, sizeof(stus[0]), cmp); for(i = 0; i < n; i++){ printf("%s %d %d\n", stus[i].name, stus[i].age, stus[i].score); } } return 0; }
0.0分
3 人评分
Stuinfo[j + 1] = myinfo; } } } } } void OutStuInfo(struct StuInfo* Stuinfo, int N){//输出 for (int i = 0; i < N; i++) { printf("%s %d %d\n", Stuinfo[i].name, Stuinfo[i].age, Stuinfo[i].score); } }
void InfoOrder(struct StuInfo* Stuinfo, int N)//排序结构体,我用了两次冒泡法,为了不同目的,一次排分数,依次排字母 { struct StuInfo myinfo; for (int i = 0; i < N - 1; i++)//排分数 { for (int j = 0; j < N - i - 1; j++) { if (Stuinfo[j].score > Stuinfo[j + 1].score) { myinfo = Stuinfo[j]; Stuinfo[j] = Stuinfo[j + 1]; Stuinfo[j + 1] = myinfo; } } } for (int i = 0; i < N - 1; i++)//排字母 { for (int j = 0; j < N - 1 - i; j++) { if (Stuinfo[j].score == Stuinfo[j + 1].score) { if (Stuinfo[j].name[0] > Stuinfo[j + 1].name[0]) { myinfo = Stuinfo[j]; Stuinfo[j] = Stuinfo[j + 1];
void InStuInfo(struct StuInfo* Stuinfo, int N) {//输入信息,利用for循环 for (int i = 0; i < N; i++) { scanf("%s %d %d", Stuinfo[i].name, &Stuinfo[i].age, &Stuinfo[i].score); } }
楼主,我的代码用CODEBLOCKS和VS都没错误,在这里运行就提示我答案错误50,我实在看不出我错在那里了,望指教。 #include <stdio.h> struct StuInfo { char name[101];//姓名 int age;//年纪 int score;//分数 }; void InStuInfo(struct StuInfo* Stuinfo, int N); void InfoOrder(struct StuInfo* Stuinfo, int N); void OutStuInfo(struct StuInfo* Stuinfo, int N); int main() { struct StuInfo Stuinfo[1000];//定义一个结构数组,最大1000 int N;//题目要求之N scanf("%d", &N); InStuInfo(Stuinfo, N); InfoOrder(Stuinfo, N); OutStuInfo(Stuinfo, N); }
#include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct student{ char name[101]; int age; int score; }student; int cmp(const void *a,const void *b){ student *c=(student *)a; student *d=(student *)b; if(c->score!=d->score) return c->score<d->score?1:-1; if(c->name!=d->name) return strcmp(c->name,d->name); return c->age<d->age?-1:1; } int main(){ int n; student a[1000]; while((scanf("%d",&n))!=EOF){ getchar(); for(int i=0;i<n;i++){ scanf("%s %d %d",a[i].name,&a[i].age,&a[i].score); getchar(); } qsort(a,n,sizeof(a[0]),cmp);
雨天炎天 2020-02-07 17:17:42 |
for(int i=0;i<n;i++) printf("%s %d %d ",a[i].name,a[i].age,a[i].score); } }为什么这样答案错误啊,求大佬指点
这题有毒啊,为什么name是101大小。少一个都不行