这道题主要考察字符串处理和输出格式
printf("%5d") 是左对齐 固定输出5个字符
printf("%-5d") 是右对齐 固定输出5个字符
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <ctype.h>
using namespace std;
#define NAMESIZE 10
#define MAXSIZE 100
struct student{
char name[20];
int numAC = 0; //ac题目数
int score = 0; //时间分,越低越好
};
bool cmpAC(struct student s1, struct student s2){
if(s1.numAC != s2.numAC)
return s1.numAC > s2.numAC;
else
return s1.score < s2.score;
}
int main(){
vector<student> que;
int n, s;
char a[10];
struct student st;
scanf("%d %d\n", &n, &s);
while(scanf("%s", st.name) != EOF){
st.score = 0;
st.numAC = 0;
for(int i=0; i<n; i++){
scanf("%s", a);
int index = 0;
if(a[index] == '-') continue; //负数不处理
else if(a[index] == '0') continue; //0也不处理
else if(a[index] >= '1' && a[index] <= '9'){
st.numAC++;
int num = 0;
int error = 0;
while(a[index]>='0' && a[index]<='9' && a[index] != '('){
num = num*10 + a[index]-'0';
index++;
}
if(a[index] == '('){
index++;
while(a[index]>='0' && a[index]<='9' && a[index] != ')'){
error = error*10 + a[index]-'0';
index++;
}
}
num = num + error*s;
st.score += num;
}
}
que.push_back(st);
}
sort(que.begin(), que.end(), cmpAC);
vector<student>::iterator it = que.begin();
for(; it != que.end(); it++){
printf("%-10s %2d %4d\n", it->name, it->numAC, it->score);
}
return 0;
}