본문 바로가기
Algorithm/백준

[백준] 파도반 수열 9461번 - java

by jackWillow 2021. 11. 7.
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class Main {	
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
		StringBuilder sb = new StringBuilder();
		long[] wave = new long[101];
		wave[1] = 1;
		wave[2] = 1;
		wave[3] = 1;
		wave[4] = 2;
		wave[5] = 2;
		int index = 5;
		
		int T = Integer.parseInt(br.readLine());
		for(int i = 0; i < T; i++) {
			int n = Integer.parseInt(br.readLine());
			if(wave[n] == 0) {
				while(index < n) {
					wave[++index] = wave[index-1] + wave[index-5];
				}
			}
			sb.append(wave[n]).append('\n');
		}
		bw.write(sb.toString());
		bw.close();
		br.close();
	}
}
반응형