백준 1193

hong030·2023년 2월 19일
0
  • 실버 5단계 문제

풀이)

1번 대각선 줄: 분모분자 합이 2, 1개
2번 대각선 줄: 분모분자 합이 3, 2개
...
i번 대각선 줄: 분모분자 합이 i+1, i개

즉 입력받는 N에 대해 몇번째 줄 몇 번째 순서에 있는지를 알아내야 한다.

몇 번째 줄인지를 알기 위해

while(cnt<N){
	line++;
	cnt=line*(line+1)/2;
}

줄 내 몇 번째 수인지 알기 위해

int num=N-(line-1)*line/2;
if(line%2==0) {
	top=num;
	bot=line-num+1;
}else {
	top=line-num+1;
	bot=num;
}

코드)

import java.io.*;

public class Backjoon1193 {
	public static void main(String[]args) throws IOException{
		BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
		int N=Integer.parseInt(bf.readLine());
		
		int line=0;
		int top=1;
		int bot=1;
		int cnt=0;
		if(N==1) {
			System.out.println("1/1");
		}else {
			while(cnt<N){
				line++;
				cnt=line*(line+1)/2; //몇번 째 행인지 찾는 코드
			}
			int num=N-(line-1)*line/2;  //행의 몇번 째 값인지 찾는 코드
			if(line%2==0) {  //짝수 행일 시 
				top=num;
				bot=line-num+1;
			}else {
				top=line-num+1;
				bot=num;
			}
			System.out.println(top+"/"+bot);
		}
	}
}

profile
자바 주력, 프론트 공부 중인 초보 개발자. / https://github.com/hongjaewonP

0개의 댓글