[アルゴリズム]Baekjun-1003(フィボナッチ関数)/Java

12387 ワード

import java.io.BufferedReader;
import java.io.InputStreamReader;

class Main {
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int TC = Integer.parseInt(br.readLine());
		StringBuilder sb = new StringBuilder();
		for (int t = 0; t < TC; t++) {
			
			
			int N = Integer.parseInt(br.readLine());
			if(N ==0) {
				sb.append(1).append(" ").append(0).append("\n");
				continue;
			}else if(N==1) {
				sb.append(0).append(" ").append(1).append("\n");
				continue;
			}
			int[][] arr = new int[N+1][2];

			// fib(0)
			arr[0][0] = 1; // 0갯수
			arr[0][1] = 0; // 1갯수

			// fib(1)
			arr[1][0] = 0;
			arr[1][1] = 1;

			for (int i = 2; i <= N; i++) {
				arr[i][0] = arr[i - 1][0] + arr[i - 2][0];
				arr[i][1] = arr[i - 1][1] + arr[i - 2][1];
			}

			sb.append(arr[N][0]).append(" ").append(arr[N][1]).append("\n");

		}
		System.out.print(sb);
	}
}