【題解】ZeroJudge d039: 11044 – Searching for Nessy

【題目敘述】https://zerojudge.tw/ShowProblem?problemid=d039
【解題想法】內建數學函數的應用

  • 邊緣的格子不需要監控,所以長邊短邊都是先減去 2,再除以 3 後無條件進位。
  • ceil( )無條件向上取整,但其回傳值資料型態為 double
#include <iostream>
#include <cmath>
using namespace std;

int main() {
    int T;
    double n, m;
    cin >> T;
    while (T--){
        cin >> n >> m;
        cout << (int)ceil((n-2)/3) * (int)ceil((m-2)/3) << endl;
    }
    return 0;
}

Python code (credit: Amy Chou)

import math

T =  int(input())
while T:
    T -= 1
    n, m = map(int, input().split())
    print(math.ceil((n - 2) / 3) * math.ceil((m - 2) / 3))
分享本文 Share with friends