백준 문제풀이/기본수학1

1193. 분수찾기 (자바, Java)

뮤츠 2022. 9. 18. 23:48

기본수학 파트가 다 그렇지만, 프로그래밍이 어려운 문제가 아니라, 수학이 힘든 문제입니다.

수열로 풀어주면 됩니다. 분자와 분모의 합이 같은 그룹을 기준으로해서, 각 첫항 혹은 끝항을 기준으로 잡아서,

한 칸 전진할 때마다, 분자분모를 1씩 바꿔줍니다.

 

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

public class Main {

	public static int[] n(int an) {
		
		int[] result = new int[2];
		int stand;
		
		for (int i=1; ; i++) {
			
			stand = (i * (i+1))/2;
			if (an <= stand) {
				
				result[0] = i;
				result[1] = stand - an;
				break;
				
			}			
		}
		
		return result;
		
	}
	
	public static void main(String[] args) throws IOException {
		
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		int x = Integer.parseInt(br.readLine());
		
		if (n(x)[0]%2 == 0) {
			
			System.out.println((n(x)[0]-n(x)[1]) + "/" + (n(x)[1]+1));
			
		} else {
			
			System.out.println((n(x)[1]+1) + "/" + (n(x)[0]-n(x)[1]));
			
		}
		
		

	}

}