Notice
Recent Posts
Recent Comments
Link
«   2025/06   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
Tags
more
Archives
Today
Total
관리 메뉴

main

[백준-1193번/Java] 분수찾기 본문

Algorithm

[백준-1193번/Java] 분수찾기

1984 2022. 11. 11. 23:50

* 공차가 등차수열

import java.util.*;
import java.io.*;

public class Main {

	public static void main(String[] args) throws IOException {

		Scanner sc = new Scanner(System.in);

		int num = sc.nextInt();
		int d = 1;
		int sum = 0;
		int remain = 0;

		while (true) {
			if (sum + d == num) {
				remain = 0;
				break;
			} else if (sum + d > num) {
				remain = num - sum;
				break;
			}
			sum += d;
			d++;
		}

		if (remain != 0) {
			if (d % 2 == 1) {
				System.out.println(d - remain + 1 + "/" + remain);
			} else {
				System.out.println(remain + "/" + (d - remain + 1));
			}
		} else {
			if (d % 2 == 1) {
				System.out.println(1 + "/" + d);
			} else {
				System.out.println(d + "/" + 1);
			}
		}

	}

}
728x90
Comments