Programmer, Rank Thyself

虽然没有A过去,但是自己学会了结构体的排序与运用。
#include
<iostream> #include <string> #include <algorithm> #include <iomanip> #include <cmath> using namespace std; structTeam { int rank ; stringname; int count ; int time ; int gm ; int Qus[7]; }; Teamt[25]; bool cmp1(Teama1,Teama2) { if(a1.count != a2.count) return a1.count > a2.count; else { if(a1.time != a2.time) return a1.time < a2.time; else { if(a1.gm != a2.gm) return a1.gm < a2.gm; else { for(int i = 0 ; i < a1.name.length() && i < a2.name.length();i++) { if( (int)a1.name[i] - (int)a2.name[1] < 0 ) return true; } return false; } } } } int main() { int n; int total = 0; while(cin>>n && n) { total++; for(int i =0; i < n;i++) { double Log = 1; t[i].count = 0; t[i].gm = 0; t[i].rank = 0; t[i].time = 0; cin>>t[i].name; for(int j =0; j < 7;j++) { cin>>t[i].Qus[j]; t[i].time += t[i].Qus[j]; if(t[i].Qus[j] != 0) { // Log += log(t[i].Qus[j]); t[i].count++; Log *= t[i].Qus[j]; } } if(t[i].count != 0) t[i].gm = (int)(pow(Log,(double)1/t[i].count)+0.5); } sort(t,t+n,cmp1); //得到排名 t[0].rank = 1; int Frank = 1; for(int i = 0; i <n-1;i++) { if(t[i].count == t[i+1].count && t[i].time == t[i].time&&t[i].gm == t[i+1].gm) { t[i+1].rank = t[i].rank; } else { t[i+1].rank = i + 2; } } cout<<"CONTEST "<<total<<endl; for(int i = 0 ; i < n; i++) { if(i < 9) { cout<<"0" <<t[i].rank<<" "<<setw(10)<<left<<t[i].name; // printf("%.2d %-10s%2d%5d%4d", // t[i].rank, t[i].name, // t[i].count, t[i].time, t[i].gm); cout<<" "<<setw(2)<<right<<t[i].count<<setw(5)<<right<<t[i].time<<setw(4) <<right<<t[i].gm; for(int j = 0; j< 7;j++) { cout<<setw(4)<<right<<t[i].Qus[j]; } cout<<endl; } else { cout<<t[i].rank<<" "<<setw(10)<<left<<t[i].name; cout<<" "<<right<<setw(2)<<t[i].count<<right<<setw(5)<<t[i].time<<right<<setw(4) <<right<<t[i].gm; for(int j = 0; j< 7;j++) { cout<<setw(4)<<right<<t[i].Qus[j]; } cout<<endl; } } } return 0; }
原文地址:https://www.cnblogs.com/T8023Y/p/3213670.html