题解 | #成绩排序#
成绩排序
https://www.nowcoder.com/practice/8e400fd9905747e4acc2aeed7240978b
没啥难度,就一字符串题目
#include <iostream> #include <string> #include <vector> #include <algorithm> #include <math.h> using namespace std; class student{ public: string name; int num; int pi; }; //升序 bool cmp1(student x,student y){ if(x.num<y.num){ return false; } else if(x.num==y.num){ return x.pi<y.pi; } else{ return true; } } //降序 bool cmp2(student x,student y){ if(x.num<y.num){ return true; } else if(x.num==y.num){ return x.pi<y.pi; } else{ return false; } } int main(){ string s; int num,f; cin >> num >> f; getline(cin , s); vector<student> arr; int count=0; while(getline(cin, s)){ student temp; string t; int flag =0; for(auto a: s){ if(a==' '){ flag=1; } else if(flag == 0) temp.name += a; else if(flag == 1) t+=a; } int c=t.length()-1; temp.num=0; for(auto a : t){ temp.num+= (a-'0')*pow(10,c); c--; } temp.pi=count; arr.push_back(temp); count ++; } if(f==1) sort(arr.begin(),arr.end(),cmp2); if(f==0) sort(arr.begin(),arr.end(),cmp1); for(auto a: arr){ cout << a.name << ' ' << a.num << endl; } }