Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

58-xxubin04 #219

Merged
merged 2 commits into from
Sep 25, 2024
Merged

58-xxubin04 #219

merged 2 commits into from
Sep 25, 2024

Conversation

xxubin04
Copy link
Member

@xxubin04 xxubin04 commented Sep 3, 2024

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

1, 2, 3 ๋”ํ•˜๊ธฐ


โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

15๋ถ„


โœจ ์ˆ˜๋„ ์ฝ”๋“œ

1. ๋ฌธ์ œ ์ดํ•ด

์ˆซ์ž๋“ค์„ 1, 2, 3์˜ ํ•ฉ์œผ๋กœ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค.

input = open(0).readline

def dp(value):
    if value == 1:
        return(1)
    elif value == 2:
        return(2)
    elif value == 3:
        return(4)
    else:
        return dp(value-3)+dp(value-2)+dp(value-1)

for i in range(n := int(input())):
    print(dp(int(input())))


1, 2, 3์€ ๋‚˜์˜ฌ ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜๊ฐ€ 1, 2, 4์ด๋‹ค.

3 ์ดํ›„์˜ ์ˆซ์ž์ธ 4๋ถ€ํ„ฐ ์‚ดํŽด๋ณด๋ฉด ๊ทœ์น™์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

4

  • 1
    • 1์˜ ๊ฒฝ์šฐ์ธ 1์— 3์„ ๋”ํ•˜๋ฉด 4๊ฐ€ ๋‚˜์˜จ๋‹ค.
    • 1+3 (1)
  • 2
    • 2์˜ ๊ฒฝ์šฐ์ธ 1+1, 2์— 2๋ฅผ ๋”ํ•˜๋ฉด 4๊ฐ€ ๋‚˜์˜จ๋‹ค.
    • 1+1+2, 2+2 (2)
  • 3
    • 3์˜ ๊ฒฝ์šฐ์ธ 1+1+1, 1+2, 2+1, 3์— 1์„ ๋”ํ•˜๋ฉด 4๊ฐ€ ๋‚˜์˜จ๋‹ค.
    • 1+1+1+1, 1+2+1, 2+1+1, 3+1 (4)

๋”ฐ๋ผ์„œ, 4๋ฅผ 1, 2, 3์˜ ๋”ํ•˜๊ธฐ ์‹์œผ๋กœ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜๋Š” 1 + 2 + 4์ด๋ฏ€๋กœ 7์ด๋‹ค.

5์˜ ๊ฒฝ์šฐ์—๋Š” 2, 3, 4์˜ ๊ฒฝ์šฐ์˜ ์ˆ˜์— ๊ฐ๊ฐ 3, 2, 1์„ ๋”ํ•ด์ฃผ๋ฉด ๋œ๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ 5์˜ ๊ฒฝ์šฐ์˜ ์ˆ˜๋Š” 2, 3, 4์˜ ๊ฒฝ์šฐ์˜ ์ˆ˜์˜ ํ•ฉ์ด๋‹ค. ๋”ฐ๋ผ์„œ, 2 + 4 + 7์ด๋ฏ€๋กœ 13์ด ๋œ๋‹ค.


๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

dp๊ฐ€ ๋„ˆ๋ฌด ์–ด๋ ค์šด๋ฐ, ์‰ฌ์šด ๋ฌธ์ œ๋กœ ๊ฐ์„ ์žก์•„๋ณด์•˜์Šต๋‹ˆ๋‹ค!

Copy link
Collaborator

@9kyo-hwang 9kyo-hwang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์š” ๋ฌธ์ œ๋Š” ํ•œ ์ˆซ์ž 5๊นŒ์ง€๋งŒ ์ฃฝ ๋‚˜์—ดํ•ด๋ณด๋ฉด ์ค‘๋ณต๋˜๋Š” ์ผ€์ด์Šค๊ฐ€ ๋ˆˆ์— ๋ณด์—ฌ์„œ ์ข€ ์‰ฝ๊ฒŒ ํ’€ ์ˆ˜ ์žˆ์ฃ  :)
๊ทธ๋ž˜์„œ 1, 2, 3์— ๋Œ€ํ•œ ์ดˆ๊ธฐ๊ฐ’๋งŒ ์„ค์ •ํ•œ ๋’ค bottom-up ํ˜•์‹์œผ๋กœ ํ‘ธ๋Š” ๊ฒƒ๋„ ๊น”๋”ํ•ฉ๋‹ˆ๋‹ค.

#include <iostream>
#include <vector>

using namespace std;

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr); cout.tie(nullptr);

	int T; cin >> T;
	while (T--)
	{
		int num; cin >> num;

		vector<int> dp(11, 0);
		dp[1] = 1; dp[2] = 2; dp[3] = 4; // ๊ธฐ๋ณธ๊ฐ’

		for (int i = 4; i <= num; i++)
			dp[i] = dp[i - 1] + dp[i - 2] + dp[i - 3];

		cout << dp[num] << "\n";
	}

	return 0;
}

elif value == 3:
return(4)
else:
return dp(value-3)+dp(value-2)+dp(value-1)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์‚ฌ์‹ค ์ด๋ ‡๊ฒŒ ์žฌ๊ท€ํ•จ์ˆ˜ ๊ฒฐ๊ณผ๋ฅผ ์ฆ‰์‹œ ๋ฆฌํ„ดํ•˜๋Š” ๊ฒƒ์€ ์—„๋ฐ€ํžˆ ๋”ฐ์ง€๋ฉด "๋‹ค์ด๋‚˜๋ฏน ํ”„๋กœ๊ทธ๋ž˜๋ฐ" ๊ธฐ๋ฒ•์€ ์ ์šฉ๋˜์ง€ ์•Š์€ ๊ฑฐ๊ธด ํ•ฉ๋‹ˆ๋‹ค. ์ค‘๋ณต๋˜๋Š” ์—ฐ์‚ฐ ๊ฒฐ๊ณผ๋ฅผ ๋ฏธ๋ฆฌ ์บ์‹ฑํ•ด๋†“๊ณ , ๋‚˜์ค‘์— ์ด๋ฅผ ๋‹ค์‹œ ํ™œ์šฉํ•ด์•ผ ํ•˜๋Š”๋ฐ ์—ฌ๊ธฐ์„œ๋Š” ์ฆ‰์‹œ ๊ฐ’์„ returnํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ค‘๋ณต ์—ฐ์‚ฐ์„ ๋งค๋ฒˆ ๋ฐ˜๋ณตํ•˜๊ฒŒ ๋˜๋‹ˆ๊นŒ์š” :)

Copy link
Collaborator

@mjj111 mjj111 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์•„๋งˆ top-down์œผ๋กœ ์žฌ๊ท€ ์‚ฌ์šฉํ•ด์„œ dp ํ‘ธ๋Š” ๋ฐฉ์‹์„ ์ตํžˆ์‹œ๋ ค๊ณ  ๋ง์”€ํ•˜์‹ ๊ฑฐ ๊ฐ™์•„์š”!
dp๋Š” ๋ฉ”๋ชจ์ œ์ด์…˜ ์‚ฌ์šฉํ•˜๋Š๋ƒ ์•ˆํ•˜๋Š๋ƒ์— ๋”ฐ๋ผ, ๋™์ž‘ ๋นˆ๋„๋ฅผ ํฌ๊ฒŒ ๋‚ฎ์ถ”๋Š”๋ฐ ์ง‘์ค‘ํ•˜๊ณ  ์žˆ์๋‹ˆ๋‹ท.

์ง€๊ธˆ ๋ฌธ์ œ๋Š” ๋ฐฑํŠธ๋ ˆํ‚น์œผ๋กœ ํ‘ธ๋Š”๊ฑฐ๋ผ DP์š”์†Œ๋ฅผ ์ฐพ๊ธฐ๋Š” ์กฐ๊ธˆ ํž˜๋“ค ๊ฒƒ ๊ฐ™๋„ค์šง
๋ฌผ๋ก  dp๋กœ ํ’€์–ด๋ณธ๋‹ค๋ฉด..
๊ธฐ์กด์— target์œผ๋กœ ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๋ฉ”๋ชจ์ œ์ด์…˜ ํ•ด์„œ ๋‹ค์Œ ๊ณ„์‚ฐ์— ํ™œ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ๊ฒ ๋„ค์šฉ

n = int(input())
answer = 0

def calculate(target, now = 0):
    global answer
    if target == now:
        answer += 1
        return
    elif now > target:
        return
    
    for i in [1, 2, 3]:
        calculate(target, now + i)

for _ in range(n):
    value = int(input())
    calculate(value)
    print(answer)
    answer = 0

Copy link
Member

@gjsk132 gjsk132 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bottom-up์œผ๋กœ ํ•ด์„œ ํ’€์—ˆ์Šต๋‹ˆ๋‹ค!
๋‚˜์˜ค๋Š” ์ •์ˆ˜์˜ ๋ฒ”์œ„๊ฐ€ ์ž‘๊ณ , ์ •์ˆ˜์— ๋”ฐ๋ผ ๊ฒฐ๊ณผ๊ฐ€ ์ •ํ•ด์ ธ์žˆ์–ด์„œ ๊ทธ๋ƒฅ ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ์ €์žฅํ•ด๋‘๊ณ , ์ถœ๋ ฅํ–ˆ์Šต๋‹ˆ๋‹ค!

input = open(0).readline

dp = [1 for _ in range(11)]

dp[0:4] = [0,1,2,4]

for i in range(4,11):
    dp[i] = dp[i-1] + dp[i-2] + dp[i-3]

for i in range(int(input())):
    num = int(input())
    print(dp[num])

๊ฐ’์— ๋Œ€ํ•œ ๊ฒฐ๊ณผ๊ฐ€ ์ •ํ•ด์ ธ์žˆ์–ด์„œ, ๊ฒฐ๊ณผ ๋‚˜์˜จ๊ฑธ ๊ทธ๋Œ€๋กœ ๋ฐฐ์—ด์— ๋„ฃ์€์ฑ„๋กœ ์‹œ์ž‘ํ•ด๋„ ๋งž์ถ”์‹ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค :)

input = open("input.txt").readline

answer = [0, 1, 2, 4, 7, 13, 24, 44, 81, 149, 274]

for i in range(int(input())):
    num = int(input())
    print(answer[num])

@9kyo-hwang 9kyo-hwang merged commit 7027e3c into AlgoLeadMe:main Sep 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants