【題解】ZeroJudge d660: 11764 – Jumping Mario

【題目敘述】https://zerojudge.tw/ShowProblem?problemid=d660
【解題想法】

  • 題目:a high jump 代表瑪莉歐(Mario)跳到一個較高的牆,同樣,a low jump代表瑪莉歐(Mario)跳到一個較矮的牆。
  • 兩道牆一樣高時:不算 high 也不計入 low。
  • 注意:牆的高度可能為 0。
#include <iostream>
using namespace std;

int main() {
    int T, N, h;
    cin >> T;
    for (int Case = 1; Case <= T; Case++){
        cin >> N;
        int cur = -1, high = 0, low = 0;
        for (int i = 0; i < N; i++){
            cin >> h;
            if (cur >= 0) {
                if (h > cur) {
                    high++;
                } else if (h < cur) {
                    low++;
                }
            }
            cur = h;
        }
        cout << "Case " << Case << ": ";
        cout << high << " " << low << "\n";
    }
    return 0;
}

Python code (credit: Amy Chou)

T = int(input())
for TC in range(1, T+1):
    N = int(input())
    h = list(map(int, input().split()))
    
    cur = -1
    low = 0
    high = 0
    for i in range(N):
        if cur >= 0:
            if h[i] > cur:
                high += 1
            elif h[i] < cur:
                low += 1
        cur = h[i]
        
    print(f"Case {TC}: {high} {low}")
分享本文 Share with friends