• Home
  • About
    • on Weekend photo

      on Weekend

      𝙎𝙩𝙪𝙙𝙮𝙞𝙣𝙜

    • Learn More
    • Instagram
    • Github
  • Archive
    • All Posts
    • All Tags
    • All Categories
  • Categories
    • Problem Solving
    • TIL
    • Study
    • Etc
    • 필사
  • Projects

[백준] 1193 분수 찾기

06 Dec 2020

코드 바로가기
‘분수 찾기’ 문제 확인

풀이

1 : 1/1
2 : 1/2, 2/1
3 : 3/1, 2/2, 1/3
4 : 1/4, 2/3, 3/2, 4/1
5 : 5/1, 5/2, 3/3, 2/4, 1/5
…
위와 같은 순서로 지그재그가 진행됩니다.
보시면 앞의 인덱스가 짝수일 때는 분자는 1부터 idx까지 증가하고, 분모는 idx부터 1까지 감소하며 홀수일 때는 분자와 분모가 서로 반대가 됩니다.
이를 아이디어로 구현하면 됩니다!

코드

#include <iostream>
#include <string>
#include <vector>
#include <deque>
#include <algorithm>

using namespace std;

int main()
{
	int X;
	cin >> X;
	int cnt = 1;
	for (int i = 1; i < 10000000; i++)
	{
		for (int j = 1; j <= i; j++) {
			if (cnt == X) {
				int bunja, bunmo;
				if (i % 2 == 0) {
					bunja = j;
					bunmo = i - j + 1;
				}
				else {
					bunja = i - j + 1;
					bunmo = j;
				}
				cout << bunja << "/" << bunmo;
				return 0;
			}
			cnt++;
		}
	}
	return 0;
}



problem_solvingc++ Share Tweet +1