a525簡單有勝負才納入分母
CPE:Rock-Paper-Scissors Tournament (UVa 10903)
分開累計每位玩家的勝與敗,正確跳過平手。
解題思路
勝率分母不是總場數,而是勝場加敗場。每場若兩個手勢相同,任何人的計數都不變;否則用 rock→scissors、scissors→paper、paper→rock 的克制表判斷勝者,再同時增加勝者的勝場與敗者的敗場。
一組固定讀取 k×n×(n−1)/2 場,不能因平手而少讀一場。輸出依編號排序,分母為零印 -;其餘先將勝率乘1000,以整數餘數四捨五入,再印固定三位小數,避免1/16=0.0625的中點誤差。只有組與組之間留一空行,n=0時不再讀k。
Step-by-step animation
示意:兩人進行三場
步驟 1 / 3
第一場
1 出 rock、2 出 paper,2 勝。
1:0勝1敗
2:1勝0敗
- 已處理
- 1
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>
#include <numeric>
#include <cmath>
#include <iomanip>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <array>
#include <tuple>
#include <functional>
#include <limits>
#include <cstdint>
#include <cctype>
#include <utility>
using namespace std;
int main() {
int n, k;
bool first = true;
map<string, string> beats{{"rock", "scissors"}, {"scissors", "paper"}, {"paper", "rock"}};
while (cin >> n && n) {
cin >> k;
vector<int> w(n + 1), l(n + 1);
for (int z = 0; z < k * n * (n - 1) / 2; ++z) {
int a, b;
string x, y;
cin >> a >> x >> b >> y;
if (x == y)
continue;
if (beats[x] != y)
swap(a, b);
++w[a];
++l[b];
}
if (!first)
cout << '\n';
first = false;
for (int i = 1; i <= n; ++i) {
if (w[i] + l[i] == 0)
cout << "-\n";
else {
int total = w[i] + l[i];
int thousandths = (1000 * w[i] + total / 2) / total;
cout << thousandths / 1000 << '.' << setw(3) << setfill('0') << thousandths % 1000
<< '\n';
}
}
}
}
先試著自己完成,再切換語言比對解法。參考程式可複製到題目編輯器。