반응형
문제
두 정수 A와 B를 입력받은 다음, A+B를 출력하는 프로그램을 작성하시오.
입력
첫째 줄에 A, 둘째 줄에 B가 주어진다. (0 < A, B < 10)
출력
첫째 줄에 A+B를 출력한다.
예제 입력 1
1
2
예제 출력 1
3
알고리즘 분류
구현(implementation), 사칙연산(arithmetic), 수학(math)
소스코드
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine());
for (int i = 0; i < T; i++) {
int n = Integer.parseInt(br.readLine());
System.out.println(call(n));
}
}
public static int call(int n) {
switch (n) {
case 1:
return 1;
case 2:
return 2;
case 3:
return 4;
}
int[] cache = new int[n + 1];
cache[1] = 1;
cache[2] = 2;
cache[3] = 4;
for (int index = 4; index <= n; index++) {
cache[index] = cache[index - 1] + cache[index - 2] + cache[index - 3];
}
return cache[n];
}
}
반응형
'코딩테스트 > 백준' 카테고리의 다른 글
JAVA 백준 2743번 단어 길이 재기 (0) | 2022.07.25 |
---|---|
JAVA 백준 2738번 행렬 덧셈 (0) | 2022.07.24 |
JAVA 백준 9095번 1, 2, 3 더하기 (0) | 2022.07.22 |
JAVA 백준 2475번 검증수 (0) | 2022.07.22 |
JAVA 백준 2420번 사파리월드 (0) | 2022.07.21 |
댓글