HD-ACM算法专攻系列(16)——考试排名
问题描述:
源码:
主要要注意输出格式.
#include"iostream" #include"iomanip" #include"algorithm" #include"string" using namespace std; struct Person { string name; int count; int score; }; bool cmp(Person a, Person b) { if(a.count > b.count) { return true; } else if(a.count == b.count) { if(a.score < b.score) { return true; } else if(a.score == b.score) { return a.name < b.name; } else { return false; } } else { return false; } } int atoi(string str, int start, int end) { int result = 0; for(int i = start; i <= end; i++) { result = result * 10 + (str[i] - '0'); } return result; } int main() { int n, m, index = 0; string str; Person *p = new Person[1000]; cin>>n>>m; while(cin>>p[index].name) { p[index].count = 0; p[index].score = 0; for(int j = 0; j < n; j++) { cin>>str; if(str[0] != '-' && str[0] != '0') { p[index].count++; if(str[str.length() - 1] == ')') { for(int k = 0; k < str.length(); k++) { if(str[k] == '(') { p[index].score += atoi(str, 0, k - 1); p[index].score += atoi(str, k+1, str.length() - 2) * m; break; } } } else { p[index].score += atoi(str, 0, str.length() - 1); } } } index++; //if(index == 6)break; } sort(p, p + index, cmp); for(int i = 0; i < index; i++) { cout<<std::left<<setw(10)<<p[i].name<<" "<<std::right<<setw(2)<<p[i].count<<" "<<setw(4)<<p[i].score<<endl; } return 0; }