본문 바로가기

Algorithm/Baekjoon

[백준_JAVA] 1193 분수찾기

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
	public static void main(String[] args) throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int input = Integer.parseInt(br.readLine()); // 입력 값
		int col = 0; // 칸의 수
		int countNum = 1; // 몇 번째 숫자
		int numerator = 0; // 분자
		int denominator = 0; // 분모
		
		while(col < input) {
			col += countNum;
			countNum++;
		}
		
		if((countNum-1)%2 == 0) {
			denominator = 1 + (col - input);
			numerator = (countNum-1) - (col-input);
		}else {
			denominator = (countNum-1) - (col-input);
			numerator = 1 + (col - input);
		}
		System.out.println(numerator+"/"+denominator);
	}
}

규칙이 이해가 잘 안 갔다