끄적끄적 코딩
article thumbnail
Published 2019. 10. 27. 23:36
[C++] 백준 16430번 제리와 톰 알고리즘

x = 1 - P/Q일 때 
x를 기약분수로 나타내는 문제입니다.


#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;

int main(int argc, char *argv[])
{
	int a, b;
	int x, y;
	int result;

	cin >> x >> y;

	b = y;
	a = y - x;

	if (x == y) {
		cout << "0 0" << endl;
	}
	else {
		int i = 1;
		while (++i) {
			if (a < i) {
				break;
			}
			if (a % i == 0 && b % i == 0) {
				a /= i;
				b /= i;
				--i;
			}
		}

		cout << a << " " << b << endl;
	}

	return 0;
}

검색 태그