알고리즘

C++ 알고리즘 - 백준 1193 분수 찾기

마루설아 2025. 1. 11. 12:46

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

 

#include <bits/stdc++.h>
#define endl "\n"

using namespace std;

int main(void) {
	/************** C++ Init **************/
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	/************** C++ Init **************/


	int input;
	int n = 1;
	int k = 1;
	int mo, ja;
	cin >> input;

	while (input > n) {
		if (input < n + k) break;
		n += k;
		k += 1;
	}

	if (k % 2 == 0) {
		mo = k;
		ja = 1;

		for (int i = n; i < input; i++) {
			mo--;
			ja++;
		}
	}

	else {
		mo = 1;
		ja = k;

		for (int i = n; i < input; i++) {
			mo++;
			ja--;
		}
	}
	
	cout << ja << "/" << mo;
}