在使用printf时,%后面跟-号表示左对齐,否则右对齐。%后跟0表示用0补齐,否则表示用空格补齐,%后跟数字表示对齐宽度。
例如:%-05s,表示宽度为5右对齐输出s,左面空余区域用0补齐。
简单题

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
#define maxn 25
#define maxl 15
#define maxp 15
struct Team
{
int id, penalty;
int tot;
int rank;
} team[maxn];
int n, p, m;
char name[maxn][maxl];
bool solve[maxn][maxp];
int wrong[maxn][maxp];
int getid(char *st)
{
for (int i = 0; i < n; i++)
if (strcmp(name[i], st) == 0)
return i;
return -1;
}
void input()
{
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
scanf("%s", name[i]);
team[i].id = i;
team[i].penalty = team[i].tot = 0;
}
scanf("%d%d", &p, &m);
memset(solve, 0, sizeof(solve));
memset(wrong, 0, sizeof(wrong));
for (int i = 0; i < m; i++)
{
int a, b;
char correct[maxl], t[maxl];
scanf("%d%d%s%s", &a, &b, correct, t);
int id = getid(t);
if (solve[id][a])
continue;
if (strcmp(correct, "Yes") == 0)
{
solve[id][a] = true;
team[id].penalty += b + wrong[id][a] * 20;
team[id].tot++;
continue;
}
wrong[id][a]++;
}
}
bool operator <(const Team &a, const Team &b)
{
if (a.tot != b.tot)
return a.tot > b.tot;
if (a.penalty != b.penalty)
return a.penalty < b.penalty;
return strcmp(name[a.id], name[b.id]) < 0;
}
void work()
{
for (int i = 0; i < n; i++)
{
if (i == 0 || team[i].tot != team[i - 1].tot || team[i].penalty
!= team[i - 1].penalty)
team[i].rank = i + 1;
else
team[i].rank = team[i - 1].rank;
printf("%2d. %-8s% 2d% 5d\n", team[i].rank, name[team[i].id], team[i].tot, team[i].penalty);
}
}
int main()
{
//freopen("t.txt", "r", stdin);
int t;
scanf("%d", &t);
while (t--)
{
input();
sort(team, team + n);
work();
putchar('\n');
}
return 0;
}