자바 1193 분수 찾기

·2023년 1월 10일

백준

목록 보기
15/16
post-thumbnail

이게 자바야 c야.. 체계적이지 못한 사람이라 그냥 주먹구구로 풀엇습니다 sry

package 기본수학1;

import java.util.Scanner;

public class Num1193 {
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    int num = scanner.nextInt();
    int group = 0;
    int res = 0;
    int i = 0;
    int bottom = 1;
    int top = 1;

    while (true) {
      if (num <= res) {
        break;
      }
      group++;
      res += group;
    }
    i = num - (res - group);
    if (group % 2 == 1) {
      top = group;
      for (int j = 1; j < i; j++) {
        top--;
        bottom++;
      }
    } else {
      bottom = group;
      for (int j = 1; j < i; j++) {
        bottom--;
        top++;
      }
    }
    System.out.println(top + "/" + bottom);
  }
}

  1. 무한루프 돌면서 돌면서 몇번 째 그룹의 몇번 째 요소인지 판별
  2. 홀, 짝 별로 증감이 다르길래 분모, 분자 증감 다르게

무한루프 없애고싶다 벅벅

profile
어?머지?

1개의 댓글

comment-user-thumbnail
2023년 1월 14일

역시.... 분수를 아시는 분..

답글 달기