創建一個結構陣列,紀錄每個人勝敗場次數,如以下程式碼:
#define SIZE 101
struct player
{
int win;
int lose;
};
struct player p[SIZE];
其實只要比較每場勝敗為誰,再將這些結果累加到每個人的陣列索引,最後印出勝率。C 語言程式碼如下:
scanf("%d", &k);
for (i = 1; i <= n; i ++)
p[i].win = 0, p[i].lose = 0;
game = k * n * (n - 1) / 2;
while (game --)
{
scanf("%d %s %d %s", &p1, f1, &p2, f2);
int kind1, kind2;
if (f1[0] == 'r') kind1 = 1;
else if (f1[0] == 'p') kind1 = 2;
else kind1 = 3;
if (f2[0] == 'r') kind2 = 1;
else if (f2[0] == 'p') kind2 = 2;
else kind2 = 3;
if (kind1 > kind2)
{
j = kind1 - kind2;
if (j == 1) p[p1].win ++, p[p2].lose ++;
else p[p2].win ++, p[p1].lose ++;
}
else if (kind1 < kind2)
{
j = kind2 - kind1;
if (j == 1) p[p2].win ++, p[p1].lose ++;
else p[p1].win ++, p[p2].lose ++;
}
}
int total, win;
for (i = 1; i <= n; i ++)
{
win = p[i].win;
total = win + p[i].lose;
if (total == 0) printf("-\n");
else printf("%.3f\n",(float)win / (float)total);
}
By David.K
p10903題目連結
回ACM題庫目錄
回首頁
沒有留言:
張貼留言