내가 푼 코드랑 다른 사람이 푼 코드랑 너무 달랐다.
나는 배열을 일일이 입력해서 풀었다면 다른 사람들은 X에 해당하는 배열의 값을 계산했다.
[참고] https://st-lab.tistory.com/74
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
/**
* 배열 [n][m]
* String[] presentPosition = n/m
*
* X번째 분수 구하기
* 현재 위치에서 m = 1 인 경우 -> 오른쪽 위로 이동.
* 오른쪽 위로 가는 경우: n이 1이 될때까지 이동. 한칸 이동 시 n—-, m++
* 이동 완료 시 m만 m++
* 현재 위치에서 n = 1 인 경우 -> 왼쪽 아래로 이동.
* 왼쪽 아래로 가는 경우: m이 1이 될때까지 이동. 한칸 이동 시 m--, n++
* 이동 완료 시 n++
*
* X번까지 진행하고 presentPosition 출력
*/
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
int x = Integer.parseInt(bf.readLine());
Matrix matrix = new Matrix(1,1);
// X번 반복
Loop1:
while(x>1) {
if(matrix.getN() == 1) {
if(matrix.getM() == 1) {
matrix.moveRight();
x--;
if(x == 1) break;
}
while(matrix.getM() > 1) {
matrix.moveLeftDown();
x--;
if(x == 1) break Loop1;
}
matrix.moveDown();
x--;
if(x == 1) break;
}
if(matrix.getM() == 1) {
while(matrix.getN() > 1) {
matrix.moveRightUp();
x--;
if(x == 1) break Loop1;
}
matrix.moveRight();
x--;
if(x == 1) break;
}
}
matrix.printMatrix();
}
}
class Matrix {
int n;
int m;
public Matrix(int n, int m) {
this.n = n;
this.m = m;
}
int getN() {
return n;
}
int getM(){
return m;
}
void moveRightUp() {
n--;
m++;
}
void moveRight() {
m++;
}
void moveLeftDown() {
n++;
m--;
}
void moveDown() {
n++;
}
void printMatrix() {
System.out.println(n + "/" + m);
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
int X = Integer.parseInt(bf.readLine());
int cross_count = 1;
int prev_count_sum = 0;
while(true){
if(X <= cross_count + prev_count_sum) {
if(cross_count % 2 == 0) { //왼쪽 아래로 이동
System.out.println((X-prev_count_sum) + "/" + (cross_count + 1 - (X - prev_count_sum)));
break;
} else { // 오른쪽 위로 이동
System.out.println((cross_count + 1 - (X - prev_count_sum))+ "/" + (X-prev_count_sum));
break;
}
} else {
prev_count_sum += cross_count;
cross_count++;
}
}
}
}