RT,感谢神犇相助。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <unordered_map>
#include <vector>
using namespace std;
const int N = 110;
const double eps = 1e-5; // avoid EOF
int idx; // student number
struct Student {
string sid; // Student ID
int cid; // Class ID
string name; // Begin with a capital letter
int score[8]; // Chinese, Math, English, Programming
bool is_exist;
} s[N], tmp[N];
int get_total(Student a) { // Calculate Student's total score
int total = 0;
for (int i = 1; i <= 4; ++i) total += a.score[i];
return total;
}
void msort(Student s[], int l, int r) { // Sort the Students
if (l >= r) return ;
int mid = l + r >> 1;
msort(s, l, mid), msort(s, mid+1, r);
int i = l, j = mid+1, k = 1;
while (i <= mid && j <= r) {
if (get_total(s[i]) >= get_total(s[j])) tmp[k ++] = s[i ++];
else tmp[k ++] = s[j ++];
}
while (i <= mid) tmp[k ++] = s[i ++];
while (j <= r) tmp[k ++] = s[j ++];
for (int i = l, j = 1; i <= r; ++i, ++j) s[i] = tmp[j];
}
void Main_Menu() { // Operations
puts("Welcome to Student Performance Management System (SPMS).");
puts("");
puts("1 - Add");
puts("2 - Remove");
puts("3 - Query");
puts("4 - Show ranking");
puts("5 - Show Statistics");
puts("0 - Exit");
puts("");
}
void Add() { // Add student(s) (operation 1)
idx ++;
puts("Please enter the SID, CID, name and four scores. Enter 0 to finish.");
cin >> s[idx].sid;
while (s[idx].sid != "0") {
cin >> s[idx].cid >> s[idx].name;
for (int i = 1; i <= 4; ++i) cin >> s[idx].score[i];
s[idx].is_exist = 1;
// If the sid has appeared
bool flag = 0;
for (int i = 1; i < idx; ++i) {
if (s[i].sid == s[idx].sid) {
flag = 1;
break;
}
}
if (flag) {
idx --; // Ignore the duplicated SID.
puts("Duplicated SID.");
}
idx ++;
printf("Please enter the SID, CID, name and four scores. ");
puts("Enter 0 to finish.");
cin >> s[idx].sid;
}
idx --;
}
void Remove() { // Remove student(s) (operation 2)
string ss;
puts("Please enter SID or name. Enter 0 to finish.");
cin >> ss;
while (ss != "0") {
int cnt = 0;
if (isdigit(ss[0])) { // User inputs a sid
// If the sid is in the queue
for (int i = 1; i <= idx; ++i) {
if (s[i].sid == ss && s[i].is_exist) {
s[i].is_exist = 0;
cnt ++;
}
}
} else { // User inputs a name
for (int i = 1; i <= idx; ++i) {
if (s[i].name == ss && s[i].is_exist) {
s[i].is_exist = 0;
cnt ++;
}
}
}
printf("%d student(s) removed.\n", cnt);
puts("Please enter SID or name. Enter 0 to finish.");
cin >> ss;
}
}
void Query() { // Query student's rank (operation 3)
string ss;
puts("Please enter SID or name. Enter 0 to finish.");
cin >> ss;
msort(s, 1, idx);
while (ss != "0") {
int cnt = 0;
if (isdigit(ss[0])) {
for (int i = 1; i <= idx; ++i) {
if (s[i].is_exist) cnt ++;
if (s[i].sid == ss && s[i].is_exist) {
cout << cnt << " " << s[i].sid << " " << s[i].cid << " ";
cout << s[i].name << " ";
for (int j = 1; j <= 4; ++j) printf ("%d ", s[i].score[j]);
printf("%d %.2f\n", get_total(s[i]), get_total(s[i])/4.0+eps);
}
}
} else {
for (int i = 1; i <= idx; ++i) {
if (s[i].is_exist) cnt ++;
if (s[i].name == ss && s[i].is_exist) {
cout << cnt << " " << s[i].sid << " " << s[i].cid << " ";
cout << s[i].name << " ";
for (int j = 1; j <= 4; ++j) printf ("%d ", s[i].score[j]);
printf("%d %.2f\n", get_total(s[i]), get_total(s[i])/4.0+eps);
}
}
}
puts("Please enter SID or name. Enter 0 to finish.");
cin >> ss;
}
}
void Show_Ranklist() { // Show the Ranklist :) (operation 4)
puts("Showing the ranklist hurts students\' self-esteem. Don\'t do that.");
}
void Show_Statistics() { // Show students' score (operation 5)
puts("Please enter class ID, 0 for the whole statistics.");
int cid;
cin >> cid;
int sum[8] = {}, pass[8] = {}, fail[8] = {};
int pass_total[8] = {};
int num = 0; // number of students
if (cid == 0) { // Counting ...
for (int i = 1; i <= idx; ++i) {
if (s[i].is_exist) {
int cnt = 0;
for (int j = 1; j <= 4; ++j) {
sum[j] += s[i].score[j];
if (s[i].score[j] >= 60) pass[j] ++, cnt ++;
else fail[j] ++;
}
pass_total[cnt] ++;
num ++;
}
}
} else {
for (int i = 1; i <= idx; ++i) {
if (s[i].cid == cid && s[i].is_exist) {
int cnt = 0;
for (int j = 1; j <= 4; ++j) {
sum[j] += s[i].score[j];
if (s[i].score[j] >= 60) pass[j] ++, cnt ++;
else fail[j] ++;
}
pass_total[cnt] ++;
num ++;
}
}
}
for (int i = 1; i <= 4; ++i) {
// Subjects
if (i == 1) puts("Chinese");
else if (i == 2) puts("Mathematics");
else if (i == 3) puts("English");
else puts("Programming");
printf("Average Score: %.2f\n", (double)(sum[i])/num);
printf("Number of passed students: %d\n", pass[i]);
printf("Number of failed students: %d\n", fail[i]);
puts("");
}
puts("Overall:");
printf("Number of students who passed all subjects: %d\n", pass_total[4]);
printf("Number of students who passed 3 or more subjects: %d\n", pass_total[3]+pass_total[4]);
printf("Number of students who passed 2 or more subjects: %d\n", pass_total[2]+pass_total[3]+pass_total[4]);
printf("Number of students who passed 1 or more subjects: %d\n", pass_total[1]+pass_total[2]+pass_total[3]+pass_total[4]);
printf("Number of students who failed all subjects: %d\n", pass_total[0]);
puts("");
}
int main() {
// freopen("UVA12412.out", "w", stdout);
Main_Menu();
int op;
while (1) {
cin >> op;
if (op == 0) break;
else if (op == 1) Add();
else if (op == 2) Remove();
else if (op == 3) Query();
else if (op == 4) Show_Ranklist();
else Show_Statistics();
Main_Menu();
}
// system("pause");
return 0;
}