C++不使用vector头文件完成:东东所在的班级有 N 名同学期末考试进行了数学、语文、英语、地理四门功课的测试。班主任要将这 N 名学生中总分前三名确定为本学期的学习小标兵。现在给出这 N 名学生的姓名和各科成绩请你编程找到总分前三名并依次输出他们的姓名。所给数据不会有总分相同的情况。输入描述共有 N+1 行第一行仅有一个正整数 N不超 40表示东东班里学生的总数接下来的 N 行每行描述一个学
#include <iostream>
#include <string>
using namespace std;
struct Student {
string name;
int math;
int chinese;
int english;
int geography;
int total;
};
int main() {
int N;
cin >> N;
Student students[N];
for (int i = 0; i < N; i++) {
cin >> students[i].name >> students[i].math >> students[i].chinese >> students[i].english >> students[i].geography;
students[i].total = students[i].math + students[i].chinese + students[i].english + students[i].geography;
}
for (int i = 0; i < N - 1; i++) {
for (int j = 0; j < N - i - 1; j++) {
if (students[j].total < students[j + 1].total) {
Student temp = students[j];
students[j] = students[j + 1];
students[j + 1] = temp;
}
}
}
cout << students[0].name << endl;
cout << students[1].name << endl;
cout << students[2].name << endl;
return 0;
}
``
原文地址: http://www.cveoy.top/t/topic/hXFg 著作权归作者所有。请勿转载和采集!