思路
读入比较麻烦,用类似读入优化的办法,读进数来以后,用结构体排序。
代码
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
struct node{
char name[101];
int ac, time;
node(){ac = 0, time = 0;}
bool operator < (const node &a)const{
return ac == a.ac ? time < a.time : ac > a.ac;
}
};
node tt[10010];
int n, p, cnt, ct, let;
char ch[1010];
inline int getint(){
char c;
int ok = 0, num = 0, op = 1;
if(ct >= let) return 0;
while((c = ch[++ct]) || 1){
if(c == '-') op = -1;
else if(c <= '9' && c >= '0') ok = 1, num = num*10 + c - '0';
else if(ok || c == '\0') return op*num;
}
}
int main(){
scanf("%d%d", &n, &p);
while(1){
if(scanf("%s", tt[++cnt].name+1) == EOF){
cnt --;
break;
}
int ac = 0, time1 = 0;
for(int i = 1; i <= n; i ++){
scanf("%s", ch+1), ct = 0;
let = strlen(ch+1);
int t = getint(), ttt;
if(t <= 0) continue;
if(t > 0){
ac ++;
ttt = getint();
time1 += (ttt*p+t);
}
// else time1 += (-1*t*p);
}
tt[cnt].ac = ac;
tt[cnt].time = time1;
}
sort(tt+1, tt+1+cnt);
for(int i = 1; i <= cnt; i ++){
printf("%-10s %2d %4d\n", tt[i].name+1, tt[i].ac, tt[i].time);
}
return 0;
}