Question
1194: 总成绩排序(结构体专题)
时间限制: 1 Sec 内存限制: 128 MB
提交: 5869 解决: 3341
有一学生成绩表,包括学号、姓名、3门课程成绩。请按如下规则排序:按总成绩降序排序,若总成绩相同,则按姓名升序排序。
输入
首先输入一个整数n(1<=n<=100),表示学生人数;
然后输入n行,每行包含一个学生的信息:学号(12位)、姓名(不含空格且不超过20位),以及3个整数,表示3门课成绩,数据之间用空格隔开。
输出
输出排序后的成绩单,格式见输出样例。
样例输入 Copy
3
541207010188 Zhangling 89 78 95
541207010189 Wangli 85 87 99
541207010190 Fangfang 89 88 85
样例输出 Copy
541207010189 Wangli 85 87 99 271
541207010190 Fangfang 89 88 85 262
541207010188 Zhangling 89 78 95 262
Code
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
const int len_chars30 = 30;
const int len_chars20 = 20;
const int scoreNums = 3;
struct StuInfo {
char ID[len_chars20];
char name[len_chars30];
//string ID;
//string name;
int score[scoreNums];
int sumScore = 0;
};
void InputStuInfo(StuInfo * stus, int n);
//void getNChars(char *, int lens);
void OutputStuInfo(StuInfo * stus, int n);
void SortStu(StuInfo * stus, int n);
int main() {
int n; // the number of students
cin >> n;
//getchar(); // get '\n';
StuInfo * stus = new StuInfo[n];
InputStuInfo(stus, n);
//OutputStuInfo(stus, n);
//cout << "++++++++++++++++++++++++++" << endl;
//getchar(); // deal with '\n'
SortStu(stus, n);
OutputStuInfo(stus, n);
delete [] stus;
return 0;
}
void InputStuInfo(StuInfo * stus, int n) {
char t;
for (int i = 0; i < n; i++) {
cin >> stus[i].ID;
cin >> stus[i].name;
//strcpy(stus[i].ID, getNChars());
//cin.get(stus[i].ID, len_chars20);
//cin.get(stus[i].name, len_chars30);
//strcpy(stus[i].name, getNChars());
//cin >> stus[i].ID >> stus[i].name;
//getchar();
//getchar();
// getNChars(stus[i].ID, len_chars20);
// getNChars(stus[i].name, len_chars30);
// cin >> stus[i].ID;
// cin >> stus[i].name;
for (int j = 0; j < scoreNums; j++) {
cin >> stus[i].score[j];
stus[i].sumScore += stus[i].score[j];
}
}
}
// void getNChars(char * temp, int lens) {
// int lenchars = lens;
// char t;
// //static char temp[len_chars30];
// for (int i = 0; i < lenchars; i++) {
// t = getchar(); // cin take no spaces, we can use getchar()
// if (t == '\n') t = getchar(); // deal with '\n', if find one take next char
// if (t != ' ') temp[i] = t;
// else {
// temp[i] = '\0';
// break;
// }
// }
// // return temp;
// }
void OutputStuInfo(StuInfo * stus, int n) {
for (int i = 0; i < n; i++) {
//getchar();
cout << stus[i].ID << " "
<< stus[i].name << " ";
for (int j = 0; j < scoreNums; j++) {
cout << stus[i].score[j] << " ";
}
cout << stus[i].sumScore << endl;
}
}
void SortStu(StuInfo * stus, int n) {
StuInfo stu_temp;
for (int i = 0; i < n - 1; i++) {
int k = i;
for (int j = i + 1; j < n; j++) {
if (stus[j].sumScore > stus[k].sumScore) k = j;
else if (stus[j].sumScore == stus[k].sumScore) {
if (strcmp(stus[j].name, stus[k].name) < 0) { // C style comparing chars using strcmp
stu_temp = stus[k];
stus[k] = stus[j];
stus[j] = stu_temp;
}
}
}
stu_temp = stus[k];
stus[k] = stus[i];
stus[i] = stu_temp;
}
}
Self-Introspection
换行符!!!
Improve my C++ skills as quickly as possible!
When I tried to use C style coding, I know one thing: I am wasting time to deal with unimportant things like which chars I would got even though I knew what I want. That kind of trifles took me plenty of time. Now is 2 : 07 am.
It’s very true that I need more practice in coding! Keep marching on, take time to do something but not waste it like just thinking without persistent action!
Time is limited. I must focus on one thing and do it continuously and persistently! Dig into myself deeply and step-by-steply.