๐Ÿ“ Coding Test Study/Algorithm Problem

[Baekjoon][C++] 9095๋ฒˆ 1, 2, 3 ๋”ํ•˜๊ธฐ ๋ฌธ์ œ์„ค๋ช… ๋ฐ ์ฝ”๋“œ

ibelieveinme 2021. 3. 7. 02:33
728x90

โ–ท ๋ฌธ์ œ์„ค๋ช…

 

9095๋ฒˆ: 1, 2, 3 ๋”ํ•˜๊ธฐ

๊ฐ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋งˆ๋‹ค, n์„ 1, 2, 3์˜ ํ•ฉ์œผ๋กœ ๋‚˜ํƒ€๋‚ด๋Š” ๋ฐฉ๋ฒ•์˜ ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

www.acmicpc.net

์ •์ˆ˜ n์ด ์ฃผ์–ด์กŒ์„ ๋•Œ, n์„ 1, 2, 3์˜ ํ•ฉ์œผ๋กœ ๋‚˜ํƒ€๋‚ด๋Š” ๋ฐฉ๋ฒ•์˜ ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜๋Š” ๋ฌธ์ œ

์ด ๋•Œ, n์˜ ๋ฒ”์œ„๋Š” 1<=n<=10์ด๋‹ค.

 

โ–ท ๋ฌธ์ œํ’€์ด

N์ด 10๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์–‘์ˆ˜์ด๊ธฐ ๋•Œ๋ฌธ์— N์€ ์ตœ๋Œ€ 10๊ฐœ ์ดํ•˜์˜ 1,2,3์˜ ์ˆ˜๋กœ ํ‘œํ˜„์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

1+1+1+1+1+1+1+1+1+1 = 10

 

1) 10์ค‘ for๋ฌธ์„ ํ™œ์šฉํ•˜์—ฌ ํ•œ ์ž๋ฆฌ์— ํ•˜๋‚˜์”ฉ ๊ฐ’์„ ๋Œ€์ž…ํ•ด๋ณด๋ฉด์„œ n์˜ ๊ฐ’๊ณผ ๊ฐ™์€์ง€๋ฅผ ํ™•์ธํ•œ๋‹ค.

n์˜ ๊ฐ’๊ณผ ๊ฐ™๋‹ค๋ฉด ํ•ด๋‹น ๊ฒฝ์šฐ๋Š” ์˜ฌ๋ฐ”๋ฅธ ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฏ€๋กœ ๋ฐฉ๋ฒ•์˜ ์ˆ˜๋ฅผ countํ•œ๋‹ค.

2) ์žฌ๊ท€ํ•จ์ˆ˜๋ฅผ ํ™œ์šฉํ•˜์—ฌ ์ธ์ž๋กœ sum, goal ๊ฐ’์„ ๋ณด๋‚ด๊ณ  ๋Œ์•„์˜ฌ ๋•Œ(์žฌ๊ท€ํ•จ์ˆ˜ return)๋งˆ๋‹ค count ํ•œ๋‹ค. 

 

โ–ท ์‹œ๊ฐ„๋ณต์žก๋„

3๊ฐ€์ง€ ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ 10๋ฒˆ ๋”ํ•˜๋ฉด ๋˜๋ฏ€๋กœ O(3*10) = O(1)์˜ ์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ ๊ฐ–๋Š”๋‹ค.

 

โ–ท ์ •๋‹ต์ฝ”๋“œ

#include <iostream>
#include <vector>
using namespace std;

void Input();
int PresentSum(int T, int v);

int main() {
	Input();
	return 0;
}

void Input() {
	int T = 0;
	cin >> T;
	for (int t = 0; t < T; t++) {
		int num = 0;
		cin >> num;
		cout << PresentSum(T, num) << endl;
	}
}

int PresentSum(int T, int num) {
	for (int t = 0; t < T; t++) {
		int count = 0;
		for (int l1 = 1; l1 <= 3; l1++) {
			if (l1 == num) {
				count++;
			}
			for (int l2 = 1; l2 <= 3; l2++) {
				if (l1 + l2 == num) {
					count++;
				}
				for (int l3 = 1; l3 <= 3; l3++) {
					if (l1 + l2 + l3 == num) {
						count++;
					}
					for (int l4 = 1; l4 <= 3; l4++) {
						if (l1 + l2 + l3 + l4 == num) {
							count++;
						}
						for (int l5 = 1; l5 <= 3; l5++) {
							if (l1 + l2 + l3 + l4 + l5 == num) {
								count++;
							}
							for (int l6 = 1; l6 <= 3; l6++) {
								if (l1 + l2 + l3 + l4 + l5 + l6 == num) {
									count++;
								}
								for (int l7 = 1; l7 <= 3; l7++) {
									if (l1 + l2 + l3 + l4 + l5 + l6 + l7 == num) {
										count++;
									}
									for (int l8 = 1; l8 <= 3; l8++) {
										if (l1 + l2 + l3 + l4 + l5 + l6 + l7 + l8 == num) {
											count++;
										}
										for (int l9 = 1; l9 <= 3; l9++) {
											if (l1 + l2 + l3 + l4 + l5 + l6 + l7 + l8 + l9 == num) {
												count++;
											}
											for (int l10 = 1; l10 <= 3; l10++) {
												if (l1 + l2 + l3 + l4 + l5 + l6 + l7 + l8 + l9 + l10 == num) {
													count++;
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}
		}
		return count;
	}
}
#include <iostream>
using namespace std;

int PresentSum(int sum, int goal);

int main() {
	int T;
	cin >> T;
	while (T--) {
		int n;
		cin >> n;
		cout << go(0, n) << '\n';
	}
	return 0;
}

int PresentSum(int sum, int goal) {
	if (sum > goal) {
		return 0;
	}
	if (sum == goal) {
		return 1;
	}
	int count = 0;
	for (int i = 1; i <= 3; i++) {
		count += go(sum + i, goal);
	}
	return now;
}
728x90