考试排名php
1 #include<iostream>
2 #include<cstring>
3 #include<cstdio>
4 #include<cmath>
5 #include<algorithm>
6 #include<map>
7 #include<set>
8 #include<vector>
9 #include<queue>
10 using namespace std; 11 #define ll long long
12 const int mod=1e9+7; 13 const int inf=1e9+7; 14
15 typedef struct
16 { 17 string name;//名字
18 int AC;//AC题数
19 int penalty;//罚时
20 } St; 21
22 vector<St>v;//考虑到人数不肯定,用不定长结构体数组(STl真香)
23
24 bool cmp(const St &a,const St &b)//按题意写cmp函数
25 { 26 if(a.AC!=b.AC)//AC题数不一样
27 return a.AC > b.AC;//题数多排名前
28 else//AC题数相同
29 { 30 if(a.penalty!=b.penalty)//罚时不一样
31 return a.penalty < b.penalty;//罚时少排名前
32 else//罚时相同
33 return a.name < b.name;//名字字典序小排名前(讲道理应该并列吧orzorz)
34 } 35 } 36
37 int main() 38 { 39 //ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
40
41 int n,time;//n个题,罚时
42
43 cin>>n>>time; 44
45 string id;//名字
46
47 St now;//定义一个结构体记录每次输入一我的名数据
48 string str;//对n个题的状况处理
49
50 while(cin>>id)//这样写读到文件尾EOF会退出循环,本身debug能够摁ctrl+z手动退出
51 { 52 now.name=id;//存名字
53 int ans=0;//计算罚时
54 int ac=0;//计算AC数
55 for(int i=0;i<n;i++) 56 { 57 cin>>str;//读入每一个题状况
58 if(str[0]=='-'||str=="0")//没作出||没作,直接跳过
59 continue; 60 ac++;//不然确定AC了
61 if(str.find("(")!=-1)//找到括号
62 { 63 int place1=str.find("("); 64 int place2=str.find(")"); 65 ans+=stoi(str.substr(0,place1));//计算两个罚时
66 ans+=stoi(str.substr(place1+1,place2-place1-1))*time; 67 } 68 else
69 { 70 ans+=stoi(str);//无括号一个罚时
71 } 72 } 73 now.AC=ac;//存AC数
74 now.penalty=ans;//存罚时
75 v.push_back(now);//导入结构体数组
76 } 77
78 sort(v.begin(),v.end(),cmp);//按题意排序
79
80 for(int i=0;i<v.size();i++) 81 { //格式输出
82 printf("%-10s %2d %4d\n",v[i].name.c_str(),v[i].AC,v[i].penalty); 83 } 84
85 return 0; 86 }