输入:每个学生一行,先输入学号,接着是各科成绩(0~5),科目门次不定,中间用空格隔开。
输出:每个学生的平均分(一位小数)从大到小排列输出,挂科(分数<=2)2门以上的同学按门次从小到大输出。
样例:
Input:
1, 4, 5, 4, 5
2, 5
3, 2, 2, 1
4, 2, 1, 3
Output:
平均分:
2, 5
1, 4.5
4, 2
3, 1.7
挂科:
4, 2
3, 3
#include <iostream> #include <string> #include <algorithm> #include <vector> const int MAXN = 100; using namespace std; struct student { int ID; float average; int numFail; }; student stu[100]; bool compare_aver(student stu1, student stu2){ if(stu1.average != stu2.average) return (stu1.average > stu2.average); } bool compare_failed(student stu1, student stu2){ if(stu1.numFail != stu2.numFail) return (stu1.numFail > stu2.numFail); } int main(){ string str; freopen("F:\input.txt", "r", stdin); int count = 0; while(cin>>str){ // int ID = str[0]; student stu1; int sum = 0, numfailed = 0; for(int i = 2; i < str.length(); i=i+2){ int number = stoi(str.substr(i, 1)); if(number <= 2) numfailed++; sum += number; } float aver = (float)sum*2/(str.length()-1); stu1.ID = str[0] - '0'; stu1.average = aver; stu1.numFail = numfailed; stu[count] = stu1; count ++; } sort(stu, stu + count, compare_aver); for(int i = 0; i < count; ++i){ cout<< stu[i].ID<< "," << stu[i].average<< endl; } sort(stu, stu + count, compare_failed); for(int i = 0; i < count; ++i){ if(stu[i].numFail != 0) cout<< stu[i].ID<< "," << stu[i].numFail<< endl; } return 0; }