[UVa #10550] Combination Lock

tolelom·2022년 7월 3일
0

UVa

목록 보기
4/20

문제 설명

문제 링크
자물쇠가 있고 자물쇠를 푸는 방법이 설명되어 있다.
주어진 방법대로 자물쇠를 풀 때 총 자물쇠를 몇 도 돌려야하는지 풀면 되는 문제

알고리즘

자물쇠를
1. 시계 방향으로 aa에서 bb로 돌릴 때 ((ab+40)%40)9((a-b+40)\%40)* 9
2. 시계 반대 방향으로 aa에서 bb로 돌릴 때 ((ba+40)%40)9((b-a+40)\%40)*9
로 계산하면 된다.

코드

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define INF 1000000000


int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    while (true) {
        int a, b, c, d;
        cin >> a >> b >> c >> d;

        if (a == 0 && b == 0 && c == 0 && d == 0) break;

        int ans = 1080;
        int pos;

        pos = a;
        ans += ((pos - b + 40) % 40) * 9;
        pos = b;
        ans += ((c - pos + 40) % 40) * 9;
        pos = c;
        ans += ((pos - d + 40) % 40) * 9;
        pos = d;

        cout << ans << '\n';

    }
}

느낀 점

moduler 연산, 구현 굿

profile
이것 저것 작성하는 기술 블로그

0개의 댓글