-
[3787] Count on CantonBOJ 2022. 4. 9. 17:19
https://www.acmicpc.net/problem/3787
3787번: Count on Canton
One of the famous proofs of modern mathematics is Georg Cantor's demonstration that the set of rational numbers is enumerable. The proof works by using an explicit enumeration of rational numbers as shown in the diagram below. 1/1 1/2 1/3 1/4 1/5 ... 2/1 2
www.acmicpc.net
짝수행이냐 홀수행이냐에 의해 거슬러 올라가거나, 내려가야 한다.
같은 행에서는 분모와 분자의 절대값의 합이 동일하니, 실제로는 분모와 분자만 바꾸어 주어도 되겠다.
#include <bits/stdc++.h>using namespace std;#ifdef ONLINE_JUDGEconstexpr bool local = false;#elseconstexpr bool local = true;#endifusing ll = long long;using pi = pair<ll, ll>;int n;int main(void) {if (!local) ios_base::sync_with_stdio(0), cin.tie(0);while (cin >> n) {cout << "TERM " << n << " IS ";int i, r, sum = 0;for (i = 1;; i++) {sum += i;if (sum >= n) {r = i;break;}}if (r & 1) {int up = sum + 1 - n;int down = -up + r + 1;cout << up << '/' << down << '\n';} else {int down = sum + 1 - n;int up = -down + r + 1;cout << up << '/' << down << '\n';}}return 0;}
'BOJ' 카테고리의 다른 글
[3584] 가장 가까운 공통 조상 (0) 2022.04.15 [12789] 도키도키 간식드리미 (0) 2022.04.12 [5525] IOIOI (0) 2022.04.07 [9375] 패션왕 신해빈 (0) 2022.04.07 [18111] 마인크래프트 (0) 2022.04.03