백준 - 1193번(분수찾기)

최지홍·2022년 2월 5일
0

백준

목록 보기
25/145

문제 출처: https://www.acmicpc.net/problem/1193


import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int X = scanner.nextInt();
        int temp = 1;
        int x = 1; // 분자
        int y = 1; // 분모
        int count = 1;

        while (temp < X) {
            temp += ++count;
        }
        
        int start = temp - count + 1; // 해당 층의 시작 번호
        int result = X - start + 1;
        if (count % 2 == 0) { // 짝수층의 경우
            for (int i = 1; i <= count; i++) {
                x = i;
                y = (count + 1 - i);
                if (i == result) {
                    break;
                }
            }
        } else { // 홀수층의 경우
            for (int i = 1; i <= count; i++) {
                x = (count + 1 - i);
                y = i;
                if (i == result) {
                    break;
                }
            }
        }

        System.out.println(x + "/" + y);
    }

}

  • 규칙성을 찾기가 조금 까다로웠다.
  • 규칙성을 찾고 나니 구현은 크게 어렵지 않았다.
profile
백엔드 개발자가 되자!

0개의 댓글