- 코드
정답 코드 : 문제만 보면 초기값만 다른 피보나치 문제란걸 알 수 있다. 첫 번째 수를 first 두번째 수를 second 라고 하면 일차 방정식이 세워진다.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int d = Integer.parseInt(st.nextToken());
int k = Integer.parseInt(st.nextToken());
int[][] array = new int[30][2];
array[0][0] = array[1][1] = 1;
array[0][1] = array[1][0] = 0;
for(int i=2; i<array.length; i++) {
array[i][0] = array[i-1][0] + array[i-2][0];
array[i][1] = array[i-1][1] + array[i-2][1];
}
int x = array[d-1][0];
int y = array[d-1][1];
int second = k/y;
int first = 0;
boolean check = false;
while(!check) {
first = k - (second * y);
if(first%x==0) {
first /= x;
check = true;
}else {
second--;
}
}
System.out.println(first);
System.out.println(second);
}
}
'algorithm' 카테고리의 다른 글
[JAVA] 백준 11724번 : 연결 요소의 개수 (0) | 2020.09.08 |
---|---|
[JAVA] 백준 1260번 : DFS와 BFS (0) | 2020.09.08 |
[JAVA] 백준 3745번 : 오름세 (0) | 2020.09.07 |
[JAVA] 백준 13398번 : 연속합 2 (0) | 2020.09.04 |
[JAVA] 백준 2195번 : 문자열 복사 (0) | 2020.09.03 |