티스토리 뷰

백준

백준 소스코드 [C++] 15989 1, 2, 3 더하기 4

Hani_Levenshtein 2020. 10. 28. 09:11

www.acmicpc.net/problem/15989

 

15989번: 1, 2, 3 더하기 4

정수 4를 1, 2, 3의 합으로 나타내는 방법은 총 4가지가 있다. 합을 나타낼 때는 수를 1개 이상 사용해야 한다. 합을 이루고 있는 수의 순서만 다른 것은 같은 것으로 친다. 1+1+1+1 2+1+1 (1+1+2, 1+2+1) 2+2

www.acmicpc.net

백준 소스코드 [C++] 15989 1, 2, 3 더하기 4

#include <iostream>
#include <algorithm>
#include <queue>
#include <string.h>
#include <limits.h>
#include <vector>
#include <math.h>
#include <stack>
#include <bitset>
#include <string>
typedef long long ll;
using namespace std;
int arr[10001];

void solve(int n) {
	for (int i = 4;i <= n;i++) {
		int carry = (i + 4) / 6;
		if (i % 6 == 0) carry++;
		arr[i] = arr[i - 1] + carry;
	}
	cout << arr[n] << '\n';
	return;
}
int main() {
	ios_base::sync_with_stdio(false), cin.tie(nullptr);
	int t, n;
	cin >> t;
	arr[1] = 1;
	arr[2] = 2;
	arr[3] = 3;

	while (t--) {
		cin >> n;
		solve(n);
	}
	
	return 0;
}
댓글