https://www.acmicpc.net/problem/9573
암호키의 각 자리수는 2씩 오차를 가질 수 있으므로 -2,-1,0,1,2의 다섯가지 경우이나 n의 범위가 5보다 작은 경우엔 항상 열 수 있게 되므로
주의하여 예외조건을 처리하면 된다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | #include <iostream> using namespace std; int n; int check(int a, int b) { int cnt = 0; for (int i = -2; i <= 2; i++) { int x = a + i; if (x > n) x -= n; else if (x <= 0) x += n; for (int j = -2; j <= 2; j ++ ) { int y = (b + j); if (y > n) y -= n; else if (y <= 0) y += n; if (x == y) { //cout << x << ' ' << y<<'\n'; cnt++; } } } return cnt; } int main() { cin>> n; int ans; int a[3], b[3]; int cnt[3] = { 0, }; int wrong = 1; for(int i=0;i<3;i++) cin >> a[i]; for (int i = 0; i<3; i++) cin >> b[i]; for (int i = 0; i < 3; i++) { cnt[i]=check(a[i], b[i]); } for (int i = 0; i < 3; i++) { wrong *= cnt[i]; } if (n >= 5) ans = 250-wrong; else ans = n * n*n; cout << ans << '\n'; } | cs |
'알고리즘 > 구현 및 시뮬레이션' 카테고리의 다른 글
[B9882] - Balanced Teams (0) | 2018.09.24 |
---|---|
[B10657] - Cow Jog (0) | 2018.09.23 |
[B10675] - Cow Routing (0) | 2018.09.23 |
[B10752] - Cow Hopscotch (0) | 2018.09.22 |
[B10025] - 게으른 백곰 (0) | 2018.09.22 |