首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> C++ >> C++ 入門

跑得快計分程序:HUNNU Contest 11458 題解

     編輯:C++ 入門

題目背景

四人玩「跑得快」,每人 12 張牌。最先出完獲勝,其餘按剩餘牌數 × 0.2 元計算輸贏,向 0.5 取整(0.4→0.5, 0.2→0)。特殊規則:未出一張牌按輸 6 元計算。輸出各人輸贏值 × 2(保證為整數)。

計分邏輯解析

單局得分計算:

  • 剩餘牌數 a[i] ∈ (0,12):基礎分 = a[i] * 0.2,向 0.5 取整後取負
  • 剩餘牌數 = 12(未出牌):固定 -6 元
  • 剩餘牌數 = 0(獲勝者):0 元

取整規則實現: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

核心難點總結

  1. 向 0.5 取整的精確實現
  2. 「未出牌」特殊規則處理
  3. 多局累積與最終輸出格式控制

本題屬於模擬題,關鍵在於細節邏輯的正確翻譯,建議構建清晰的計分函數提高可讀性。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved