티스토리 뷰
문제 링크
https://www.acmicpc.net/problem/9095
9095번: 1, 2, 3 더하기
문제 정수 4를 1, 2, 3의 합으로 나타내는 방법은 총 7가지가 있다. 합을 나타낼 때는 수를 1개 이상 사용해야 한다. 1+1+1+1 1+1+2 1+2+1 2+1+1 2+2 1+3 3+1 정수 n이 주어졌을 때, n을 1, 2, 3의 합으로 나타내는
www.acmicpc.net

N이 1일 때는 1, 2일 때는 2, 3일 때는 4가지 방법이 있습니다.
N이 4일 때는
3일 때에서 1씩 더하는 방법
2일 때에서 2씩 더하는 방법
1일 때에서 3씩 더하는 방법이 총 방법의 수 입니다.

N이 5일 때도 마찬가지로
N이 2, 3, 4일 때의 방법의 합과 같습니다.
따라서 N이 4 이상일 때에는 dp(N-1) + dp(N-2) + dp(N-3) 으로 구할 수 있습니다.
구현 코드
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#define endl '\n' | |
#define fastio cin.sync_with_stdio(false); cin.tie(nullptr); | |
using namespace std; | |
int d[11]; | |
int dp(int n){ | |
if(n==1) return 1; | |
if(n==2) return 2; | |
if(n==3) return 4; | |
if(d[n]!=0) return d[n]; | |
return d[n] = dp(n-1) + dp(n-2) + dp(n-3); | |
} | |
int main(){ | |
fastio; | |
int test_case,n; | |
cin >> test_case; | |
while(test_case--){ | |
cin >> n; | |
cout << dp(n) << endl; | |
} | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
test_case = int(input()) | |
d = [] | |
d.append(0) | |
d.append(1) | |
d.append(2) | |
d.append(4) | |
for i in range(4,11): | |
d.append(d[i-1] + d[i-2] + d[i-3]) | |
for _ in range(test_case): | |
n = int(input()) | |
print(d[n]) |
'Coding Test > 백준' 카테고리의 다른 글
[C++/Python] 백준 11057 - 오르막 수 (0) | 2020.07.27 |
---|---|
[C++/Python] 백준 10844 - 쉬운 계단 수 (0) | 2020.07.24 |
[C++/Python] 백준 11727 - 2xn 타일링 2 (0) | 2020.07.22 |
[C++/Python] 백준 11726 - 2xn 타일링 (0) | 2020.07.21 |
[C++/Python] 백준 1463 - 1로 만들기 (0) | 2020.07.20 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- C++
- 백준
- 2225
- 팰린드롬 구하기
- pyspark
- Hadoop
- 하이브
- 두니발 박사의 탈옥
- 합친 lis
- 외발 뛰기
- Django
- HDFS
- 알고스팟
- hive
- 분할정복
- 하둡
- microwaving lunch boxes
- 완전탐색
- python
- 코딩인터뷰 완전분석
- Jaeha's Safe
- Sqoop
- 종만북
- 삼각형 위의 최대 경로
- import
- 삼각형 위의 최대 경로 수 세기
- 스파크
- 출전 순서 정하기
- HiveQL
- 배열과 문자열
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
글 보관함