구현(Implementation)

백준 1598번: 꼬리를 무는 숫자 나열

cepiloth 2018. 6. 13. 12:28
반응형

https://www.acmicpc.net/problem/1598


1. 문제 요약

두 수가 주어지고 두 수간의 거리를 구하는 문제


2. 알고리즘

입력 받은 두 정수 중 작은 값에서 큰 값으로 4를 더하면서 카운팅을 한다.

작은 값에 값이 큰 값보다 커지면 종료 하고

두수의 차이를 더해주면 높이가 된다.


11 33 을 예를 들자면


1 - 15 / 33

2 - 19 / 33

3 - 23 / 33

4 - 27 / 33

5 - 31 / 33

6 - 35 / 33 -------------> 큰 값이 더 작기 때문에 종료


답은 6 + (35 - 33) = 8


3. 비고

설명이 부족하고 논리적이지 않다. 접근을 감으로 해서 그런지 쉽게 잊을 거 같다.


4. 코드

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
#include <iostream>
#include <algorithm> // min
#include <functional>
#include <math.h>
#include <string>
#include <string.h>
#include <vector>
#include <map>
#include <sstream>
#include <queue>
using namespace std;
 
int main() {
 
    std::ios::sync_with_stdio(false); cin.tie(0);
 
    int pos1;
    int pos2;
    int sol = 0;
    cin >> pos1 >> pos2;
    pos1--;
    pos2--;
    if (pos1 > pos2) swap(pos1, pos2);
    while ((pos1 / 4!= (pos2 / 4)){
        sol++;
        pos1 += 4;
 
    }
    cout << sol + abs(pos1 - pos2) << endl;
 
    return 0;
}
cs

반응형