본문 바로가기

USACO/Bronze

백준 14530 The Lost Cow (USACO US Open 2017 Bronze 1번)

문제 링크: https://www.acmicpc.net/problem/14530

 

14530번: The Lost Cow

The single line of input contains two distinct space-separated integers \(x\) and \(y\). Both are in the range \(0 \ldots 1,000\).

www.acmicpc.net

문제에서 최대 시작점과 도착점 사이의 거리의 9배만큼 걸린다고 했고, 시작점과 도착점 모두 0 부터 1000 사이의 값이므로, 실제 도착점에 도착할때까지 시뮬레이션으로 돌림.

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
#include <bits/stdc++.h>
using namespace std;
 
int x, y;
 
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
 
    cin >> x >> y;
 
    int ans = 0, cur = x;
    int two = 1, dir = 1;
    while(1) {
        while(1) {
            if(cur == y) {
                cout << ans << '\n';
                return 0;
            }
            if(cur == x + dir * two) break;
            ans++;
            cur += dir;
        }
        dir *= -1;
        two *= 2;
    }
 
    return 0;
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter