四人玩「跑得快」,每人 12 張牌。最先出完獲勝,其餘按剩餘牌數 × 0.2 元計算輸贏,向 0.5 取整(0.4→0.5, 0.2→0)。特殊規則:未出一張牌按輸 6 元計算。輸出各人輸贏值 × 2(保證為整數)。
單局得分計算:
a[i] ∈ (0,12):基礎分 = a[i] * 0.2,向 0.5 取整後取負取整規則實現:a[i] / 5 得整數部分,a[i] % 5 餘數決定小數調整:
int a_i = a[i] / 5; // 整數部分 int a_j = a[i] % 5; // 餘數 if (a_j == 1) a_j = 0; // 0.2 → 0 else if (a_j == 2) a_j = 1; // 0.4/0.5 → 0.5 else if (a_j == 3) a_j = 1; // 0.6 → 0.5 else if (a_j == 4) a_j = 2; // 0.8/1.0 → 1.0 score = -(a_i * 2 + a_j); // ×2 後取負
#include#include #include using namespace std; int main() { int t, n, a[5], a_a[5], c[5]; scanf("%d", &t); while (t--) { scanf("%d", &n); memset(c, 0, sizeof(c)); // 累積總分 while (n--) { memset(a, 0, sizeof(a)); memset(a_a, 0, sizeof(a_a)); for (int i = 0; i < 4; ++i) scanf("%d", &a[i]); for (int i = 0; i < 4; ++i) { if (a[i] > 0 && a[i] < 12) { int a_i = a[i] / 5; int a_j = a[i] % 5; if (a_j == 1) a_j = 0; else if (a_j == 2) a_j = 1; else if (a_j == 3) a_j = 1; else if (a_j == 4) a_j = 2; a_a[i] = -(a_i * 2 + a_j); } if (a[i] == 12) a_a[i] = -12; // -6 * 2 } for (int i = 0; i < 4; ++i) c[i] += a_a[i]; } printf("%d %d %d %d\n", c[0], c[1], c[2], c[3]); } return 0; }
輸入:
3 1 0 1 1 1 3 0 12 12 12 12 0 2 3 12 0 12 6 2 0 1 2 3 1 0 1 1
輸出:
0 0 0 0 12 28 -25 -15 2 0 -1 -1
本題屬於模擬題,關鍵在於細節邏輯的正確翻譯,建議構建清晰的計分函數提高可讀性。