#include<iostream> #include<algorithm> using namespace std; //定义结构体变量 struct phone { string name; int nature; double price; }p[100]; //或者单独定义:struct phone p1[100] ,p2[100] ; bool cmp(phone a,phone b) { return a.nature/a.price > b.nature/b.price;//从大到小 } int main() { int t,n; cin>>t; while(t--) { cin>>n; for(int i=0;i<n;i++) { cin>>p[i].name>>p[i].nature>>p[i].price; } sort(p,p+n,cmp); for(int j=0;j<n;j++) { cout<<j+1<<" "<<p[j].name<<" " <<p[j].nature<<" "<<p[j].price<<endl; } } } |
Double click to view unformatted code.