#include<iostream> #include<algorithm> using namespace std; struct stu { string name; double p; double q; }stud[100];//数组名和结构体类型名不要相同,不然cmp函数形参会无法识别 bool cmp(stu a, stu b) { return a.p / a.q > b.p / b.q; } int main() { int t, n,a[100]; cin >> t; while (t--) { cin >> n; for (int i = 0; i < n; i++) { cin >> stud[i].name >> stud[i].p >> stud[i].q; } sort(stud,stud+n,cmp); int k = 1; for (int i = 0; i < n; i++) { cout << k++ << " " << stud[i].name << " " << stud[i].p <<" "<< stud[i].q << endl; } } } |
Double click to view unformatted code.