https://www.acmicpc.net/problem/1003
1003번: 피보나치 함수
각 테스트 케이스마다 0이 출력되는 횟수와 1이 출력되는 횟수를 공백으로 구분해서 출력한다.
www.acmicpc.net
import java.io.*;
import java.util.*;
public class Main {
static int[] fivoarr;
static int[] fivocnt0;
static int[] fivocnt1;
public static void main(String[] args) throws IOException{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
for(int i=0; i<n; i++) {
fivoarr = new int[41];
fivocnt0 = new int[41];
fivocnt1 = new int[41];
fivoarr[0] = 0;
fivoarr[1] = 1;
fivocnt0[0] = 1;
fivocnt1[1] = 1;
st = new StringTokenizer(br.readLine());
int num = Integer.parseInt(st.nextToken());
fivo(num);
System.out.println(fivocnt0[num] + " " + fivocnt1[num]);
}
}
static int fivo(int a) {
if(a == 0) {return 0;}
if(a == 1) {return 1;}
if(fivoarr[a]==0) {
fivoarr[a] = fivo(a-2)+fivo(a-1);
fivocnt0[a] = fivocnt0[a-2]+fivocnt0[a-1];
fivocnt1[a] = fivocnt1[a-2]+fivocnt1[a-1];
return fivoarr[a];
}
return fivoarr[a];
}
}
'백준' 카테고리의 다른 글
백준 동적계획법 - 1904번 : 01타일 (0) | 2022.02.16 |
---|---|
백준 동적계획법 - 9184번 : 신나는 함수 실행 (0) | 2022.02.16 |
백준 백트래킹 - 14889번 : 스타트와 링크 (1) | 2022.02.16 |
백준 백트래킹 - 14888번 : 연산자 끼워넣기 (0) | 2022.02.15 |
백준 백트래킹 - 2580번 : 스도쿠 (1) | 2022.02.14 |