구현(Implementation)

백준 1193번 : 분수찾기

cepiloth 2018. 6. 17. 09:49
반응형

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



1. 문제 요약

각 번호의 분수들이 어떤 규칙을 있는지 유추하는 문제


2. 알고리즘

분수의 높이를 구한다.

1 -> 1/1

2 -> 2/1 1/2

3 -> 3/1 2/2 1/3

4 -> 4/1 3/2 2/3 1/4


입력받은 정수에서 높이를 뺀다.


높이가 2의 약수, 약수가 아닐 때에 따라 분모 분자의 차이를 출력 한다.


3. 코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <sstream>
#include <string>
 
using namespace std;
 
int main() {
    std::ios::sync_with_stdio(false); cin.tie(0);
 
    int x; cin >> x;
 
    int i=0;
    for (i = 1; x > 0; i++) {
        x = x - i;
    }
 
    if (i%2 == 1) {
        cout << i + x-1 << "/" << 1 - x << endl;
    } else if (i % 2 == 0) {
        cout << 1-<< "/" << i+x-1 << endl;
    }
 
    return 0;
}
cs

반응형