HD-ACM算法專攻系列(17)——考試排名
阿新 • • 發佈:2017-06-27
+= bsp esp clas 排名 描述 cpp count person
問題描述:
源碼:
主要要註意輸出格式.
#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; }
HD-ACM算法專攻系列(17)——考試排名