9095번: 1, 2, 3 더하기
각 테스트 케이스마다, n을 1, 2, 3의 합으로 나타내는 방법의 수를 출력한다.
www.acmicpc.net
#include <iostream>
using namespace std;
int d[11];
void cal() {
d[0] = 1;
for (int i = 1; i < 11; i++) {
if (i >= 1) {
d[i] += d[i - 1];
}
if (i >= 2) {
d[i] += d[i - 2];
}
if (i >= 3) {
d[i] += d[i - 3];
}
}
}
auto main()->int {
ios_base::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
cal();
int t; cin >> t;
while (t--) {
int n; cin >> n;
cout << d[n] << endl;
}
}
#include <cstdio>
int main() {
int t;
scanf("%d",&t);
while (t--) {
int ans = 0;
int n;
scanf("%d",&n);
for (int l1=1; l1<=3; l1++) {
if (l1 == n) {
ans += 1;
}
for (int l2=1; l2<=3; l2++) {
if (l1+l2 == n) {
ans += 1;
}
for (int l3=1; l3<=3; l3++) {
if (l1+l2+l3 == n) {
ans += 1;
}
for (int l4=1; l4<=3; l4++) {
if (l1+l2+l3+l4 == n) {
ans += 1;
}
for (int l5=1; l5<=3; l5++) {
if (l1+l2+l3+l4+l5 == n) {
ans += 1;
}
for (int l6=1; l6<=3; l6++) {
if (l1+l2+l3+l4+l5+l6 == n) {
ans += 1;
}
for (int l7=1; l7<=3; l7++) {
if (l1+l2+l3+l4+l5+l6+l7 == n) {
ans += 1;
}
for (int l8=1; l8<=3; l8++) {
if (l1+l2+l3+l4+l5+l6+l7+l8 == n) {
ans += 1;
}
for (int l9=1; l9<=3; l9++) {
if (l1+l2+l3+l4+l5+l6+l7+l8+l9 == n) {
ans += 1;
}
for (int l0=1; l0<=3; l0++) {
if (l1+l2+l3+l4+l5+l6+l7+l8+l9+l0 == n) {
ans += 1;
}
}
}
}
}
}
}
}
}
}
}
printf("%d\n",ans);
}
return 0;
}
'Problem set' 카테고리의 다른 글
[백준] 15650 N과 M (2) (0) | 2021.01.21 |
---|---|
[백준] 15649 N과 M (1) (0) | 2021.01.21 |
[백준] 1748 수 이어쓰기 (0) | 2021.01.19 |
[백준] 6064 카잉 달력 (0) | 2021.01.19 |
[백준] 14500 테트로미노 (0) | 2021.01.19 |