熱門文章

2026年1月23日 星期五

ZeroJudge 解題筆記:c006. 10550 - Combination Lock

作者:王一哲
日期:2026年1月23日


ZeroJudge 題目連結:c006. 10550 - Combination Lock

解題想法


刻度差 1 要轉 9 度,順時鐘方向轉刻度減少,逆時鐘方向轉刻度增加。

Python 程式碼


使用時間約為 8 ms,記憶體約為 2.8 MB,通過測試。
import sys

for line in sys.stdin:
    if line.rstrip() == "0 0 0 0":
        break
    a, b, c, d = map(int, line.split())
    tot = 360*3
    tot += 360 - (9*b + 360 - 9*a) % 360
    tot += (9*c + 360 - 9*b) % 360
    tot += 360 - (9*d + 360 - 9*c) % 360
    print(tot)


C++ 程式碼


使用時間約為 1 ms,記憶體約為 76 kB,通過測試。
#include <cstdio>

int main() {
    int a, b, c, d;
    while(scanf("%d %d %d %d", &a, &b, &c, &d) != EOF) {
        if (a == 0 && b == 0 && c == 0 && d == 0) break;
        int tot = 360*3;
        tot += 360 - (9*b + 360 - 9*a) % 360;
        tot += (9*c + 360 - 9*b) % 360;
        tot += 360 - (9*d + 360 - 9*c) % 360;
        printf("%d\n", tot);
    }
    return 0;
}


沒有留言:

張貼留言