写了PAT1026好几天,烦死人了,换了到题目写。
先说下自己的傻逼问题
1.scanf_s()这个函数的怎么输入数组不会写。。。就是因为这个想了好久好久。。。还发现堆栈溢出了,服了。。。净出些垃圾错误
2.字符数组最后有一个表示结尾的符号,占一个位置。因此8个字符要9个位置才行。。。
3.爱钻牛角尖,
4.输出的格式,不太清楚。学号是6个数,前面的0不能忽略。因此格式要为 %06d
5.scanf输入的格式也用不太会。 输入数组空格时,表示数组结束了
#include<algorithm> #include<iostream> #include<string.h> #include<cstdio> using namespace std; struct student { int ID; char name[9]; int score; }stu[100001]; bool cmpID(student a, student b) { return a.ID < b.ID; } bool cmpscore(student a, student b) { return a.score == b.score ? a.ID < b.ID : a.score < b.score;//分数相同按照名字升序来排序 } bool cmpname(student a, student b) { return strcmp(a.name, b.name) == 0 ? a.ID < b.ID : (strcmp(a.name, b.name) < 0);//名字相同就按照ID升序排序 } int main() { int N, C; scanf("%d %d", &N, &C); for (int i = 0; i < N; i++) scanf("%d%s%d", &stu[i].ID, stu[i].name, &stu[i].score);//输入学生信息 switch (C) { case 1:sort(stu, stu + N, cmpID);//递增方式排序ID break; case 2:sort(stu, stu + N, cmpname);//递减方式排序名字 break; case 3:sort(stu, stu + N, cmpscore);//递减方式排序成绩 break; default:break; } for (int i = 0; i < N; i++) printf("%06d %s %d\n", stu[i].ID, stu[i].name, stu[i].score); return 0; }