排序水题,但是我还是WA了好几次,原因是name相同时的排名结果出现了问题,建议用strng处理name输入
#include <iostream>
using namespace std;
struct Guy
{
int score;
int num;
string name;
}a[1000];
bool cmp(Guy a, Guy b)
{
if (a.num != b.num){
return a.num > b.num;
}
else
if (a.score != b.score){
return a.score < b.score;
}
else {
return (a.name < b.name);
}
}
int main()
{
int n, m;
cin >>n>>m;
int i = 0;
while (cin >> a[i].name && a[i].name != "E"){
char temp[100];
a[i].score = 0;
a[i].num = 0;
for (int k = 0; k < n; k ++){
scanf("%s", temp);
int len = strlen(temp);
int sum = 0;
int sum1 = 0;
int check = 0;
int judge = 0;
for (int ii = 0; ii < len; ii ++){
if (temp[ii] == '-')break;
if (!check && temp[ii] >= '0' && temp[ii] <= '9'){
sum*=10;
sum += (temp[ii]-'0');
if (!judge&&temp[ii]!='0'){
a[i].num ++;
judge = 1;
}
}
if (check && temp[ii] >= '0' && temp[ii] <= '9'){
sum1*=10;
sum1 += (temp[ii]-'0');
}
if (temp[ii] == '('){
check = 1;
continue;
}
}
a[i].score += (sum+sum1*m);
}
i ++;
}
sort(a, a+i, cmp);
//printf("asasd/n");
for (int j = 0; j < i; j ++){
printf("%-10s %2d %4d/n",a[j].name.c_str(),a[j].num,a[j].score);
}
//system("pause");
return 0;
}