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

9-YIM2UL2ET #29

Merged
merged 6 commits into from
Mar 17, 2024
Merged

9-YIM2UL2ET #29

merged 6 commits into from
Mar 17, 2024

Conversation

YIM2UL2ET
Copy link
Collaborator

@YIM2UL2ET YIM2UL2ET commented Mar 7, 2024

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

BOJ 1914 - ํ•˜๋…ธ์ด ํƒ‘

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

์ตœ๋Œ€ 5์‹œ๊ฐ„ (์‹ค์งˆ์ ์ธ ํ’€์ด ์‹œ๊ฐ„ ๋Œ€๋žต 60m?)

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

1. ๋ฌธ์ œ ์„ค๋ช…

์ด ๋ฌธ์ œ๋Š” ํ•˜๋…ธ์ด ํƒ‘์˜ ์›ํŒ ๊ฐœ์ˆ˜ n์„ ์ž…๋ ฅ ๋ฐ›์•„ 1 ์žฅ๋Œ€์— ์žˆ๋Š” ์›ํŒ๋“ค์„ 3 ์žฅ๋Œ€์— ์˜ฎ๊ธธ ๋•Œ
์›ํŒ์„ ์ด๋™ํ•˜๋Š” ํšŸ์ˆ˜ k๋ฅผ 1๋ฒˆ์งธ ์ค„์— ์ถœ๋ ฅํ•˜๊ณ , 2~k+1๋ฒˆ์งธ ์ค„์— ์ด๋™ ์ˆœ์„œ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค.
๋‹จ, n์ด 20๋ณด๋‹ค ํฌ๋‹ค๋ฉด ์ด๋™ ์ˆœ์„œ๋Š” ์ถœ๋ ฅํ•˜์ง€ ์•Š๋Š”๋‹ค.

2. ๋ฌธ์ œ ํ•ด์„ ๋ฐ ๊ตฌํ˜„

1. ํ•˜๋…ธ์ด ํƒ‘ ์•Œ๊ณ ๋ฆฌ์ฆ˜

ํ•˜๋…ธ์ด ํƒ‘ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ์›Œ๋‚™ ์œ ๋ช…ํ•˜์ง€๋งŒ ๋ณต์Šตํ•  ๊ฒธ ํ•œ๋ฒˆ ์งš๊ณ  ๋„˜์–ด๊ฐ‘์‹œ๋‹ค.
3๊ฐœ์˜ ์žฅ๋Œ€ ์ค‘ ์‹œ์ž‘์ ์„ start, ์ข…์ฐฉ์ ์„end, ๊ทธ ์‚ฌ์ด๋ฅผbetween ์ด๋ผ๊ณ  ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

if ๋งŒ์•ฝ `n`์ด `1`์ด๋ผ๋ฉด ์›ํŒ์ด ํ•˜๋‚˜๋ฐ–์— ์—†๋Š” ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— ๋ฐ”๋กœ `end`์— ์˜ฎ๊ฒจ ๋„ฃ๋Š”๋‹ค. // 0๋ฒˆ ์—ฐ์‚ฐ
else {
    `n`๋ฒˆ์งธ ์›ํŒ์„ `end`์žฅ๋Œ€์— ์˜ฎ๊ธฐ๊ธฐ ์œ„ํ•ด์„œ `1`~`n-1`๋ฒˆ์งธ ์›ํŒ์„ `between`์— ์˜ฎ๊ธฐ๋Š” ์ž‘์—…์„ ํ•œ๋‹ค. (์žฌ๊ท€) // 1๋ฒˆ ์—ฐ์‚ฐ
    `n`๋ฒˆ์งธ ์›ํŒ์„ `end` ์žฅ๋Œ€์— ์˜ฎ๊ธด๋‹ค. // 2๋ฒˆ ์—ฐ์‚ฐ
    `between`์— ์žˆ๋˜ `1`~`n-1`๋ฒˆ์งธ ์›ํŒ์„ `end`์— ์˜ฎ๊ธฐ๋Š” ์ž‘์—…์„ ํ•œ๋‹ค. (์žฌ๊ท€) // 3๋ฒˆ ์—ฐ์‚ฐ
}

์ด๋ฅผ ์ฝ”๋“œ๋กœ ๊ตฌํ˜„ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. (์ง์ ‘ ์›ํŒ์„ ์˜ฎ๊ธฐ๋Š” ์ž‘์—…์€ start์™€ end๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ๊ตฌํ˜„ํ–ˆ์Šต๋‹ˆ๋‹ค.)

void Hanoi(int level, int start, int between, int end)
{
    if (level == 1) std::cout << start << " " << end << "\n";  // 0๋ฒˆ ์—ฐ์‚ฐ
    else {
        Hanoi(level-1, start, end, between);  // 1๋ฒˆ ์—ฐ์‚ฐ
        std::cout << start << " " << end << "\n";  // 2๋ฒˆ ์—ฐ์‚ฐ
        Hanoi(level-1, between, start, end);  // 3๋ฒˆ ์—ฐ์‚ฐ
    }
    return;
}

๋” ์„ค๋ช…ํ•˜๊ณ  ์‹ถ์ง€๋งŒ ํ’€์–ด ์“ฐ๋ ค๋ฉด ์ •๋ง ํ•œ ์„ธ์›” ๊ฑธ๋ฆด ๊ฒƒ ๊ฐ™๋„ค์š”..
๋งํฌ << ์ด ๋ธ”๋กœ๊ทธ์— ์„ค๋ช… ์ •๋ง ์ž˜ ๋˜์–ด ์žˆ์œผ๋‹ˆ ์ดํ•ด ์•ˆ๋˜์‹œ๋Š” ๋ถ„์€ ์—ฌ๊ธฐ ๋ณด์‹œ๋ฉด ๋  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

2. ์ž„์˜ ์ •๋ฐ€๋„ / ํฐ ์ˆ˜ ์—ฐ์‚ฐ

๋ฌผ๋ก  ์ด๊ฒƒ๋งŒ ์ถ”๊ฐ€ํ•ด ์ฃผ์–ด์„œ๋Š” ์•ˆ๋˜์ง€์š”.
์ด ์—ฐ์‚ฐ ํšŸ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•ด ์ฃผ๊ธฐ ์œ„ํ•ด์„œ mainํ•จ์ˆ˜์— result๋ฅผ ์„ ์–ธํ•ด์ค์‹œ๋‹ค.

์ €๋Š” ์ฒ˜์Œ์—๋Š” Hanoi ํ•จ์ˆ˜๋ฅผ intํ˜•์œผ๋กœ ๋ณ€๊ฒฝํ•˜์—ฌ ์—ฐ์‚ฐ์„ ํ•  ๋•Œ๋งˆ๋‹ค result๋ฅผ +1 ํ•ด์ฃผ๋Š” ๋ฐฉ์‹์œผ๋กœ ํ–ˆ์Šต๋‹ˆ๋‹ค.

int Hanoi(int level, int start, int between, int end)
{
    int result = 0;
    if (level == 1) std::cout << start << " " << end << "\n", result++;
    else {
        result += Hanoi(level-1, start, end, between);
        std::cout << start << " " << end << "\n", result++;
        result += Hanoi(level-1, between, start, end);
    }
    return result;
}

์ด๋ ‡๊ฒŒ ๋ง์ด์ฃ .

์ด ํ•จ์ˆ˜ ์ž์ฒด๋Š” ๋ฌธ์ œ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ๋‹ค๋งŒ ๋ฌธ์ œ๋ฅผ ํ’€๋ ค๋ฉด ์ € ํ•จ์ˆ˜์—๋Š” ์น˜๋ช…์ ์ธ ๋‹จ์ ์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค.
๋ฐ”๋กœ n์ด 20 ์ด์ƒ์ด์–ด๋„ ์ด๋™ ํšŸ์ˆ˜๋ฅผ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์‹œ๊ฐ„์ด ์—„์ฒญ ์˜ค๋ž˜ ๊ฑธ๋ฆฐ๋‹ค๋Š” ๊ฒƒ์ด์ฃ .
๋”ฐ๋ผ์„œ result๊ฐ’์€ ๋”ฐ๋กœ ๊ตฌํ•ด์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

๊ทธ๋ ‡๋‹ค๋ฉด result๊ฐ’์„ ๋”ฐ๋กœ ๊ตฌํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ๋ฌด์—‡์ธ์ง€ ๊ถ๋ฆฌํ•ด๋ด…์‹œ๋‹ค.
๊ฐ€์žฅ ์ฒ ์ €ํ•œ ๋ฐฉ๋ฒ•์€ ์žฌ๊ท€ํ•จ์ˆ˜ ์‹คํ–‰ ๋ฐฉ์‹์„ ๋ถ„์„ํ•ด์„œ ํ•˜๋Š” ๊ฒƒ์ด์ง€๋งŒ, ์ €๋Š” 1๋ถ€ํ„ฐ ์ถœ๋ ฅํ•ด์„œ ๊ทœ์น™์„ฑ์„ ์กฐ์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค.

n result
1 1
2 3
3 7
4 15
5 31

๊ฐ์ด ๋น ๋ฅด๊ฒŒ ์˜ค์‹œ์ฃ ? 2^n-1์„ ํ†ตํ•ด์„œ result๊ฐ’์„ ๊ตฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

... n์ด 64 ์ดํ•˜์ผ๋• ๋ง์ด์ฃ ..
(unsigned long longํ˜•๋„ 2^64-1์˜ ์ˆ˜๊นŒ์ง€๋งŒ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ n์ด 64๋ฅผ ๋„˜์–ด๊ฐ€๋ฉด ์˜ค๋ฒ„ํ”Œ๋กœ์šฐ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.)
์ž ๊ทธ๋ ‡๋‹ค๋ฉด ์ง์ ‘ 2๋ฅผ ์ตœ๋Œ€ 100๋ฒˆ ์ œ๊ณฑํ•˜๊ธฐ ์œ„ํ•ด์„œ string ๊ฐ์ฒด๋ฅผ ์ด์šฉํ•˜์—ฌ ๋กœ์ง์„ ์งœ๋ด…์‹œ๋‹ค.
๊ทธ ์ „์— ๊ณฑ์…ˆ์„ ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ถ„์„ํ•ด๋ด…์‹œ๋‹ค.

์šฐ์„  ํ•จ์ˆ˜ ํ‹€์„ ๋งŒ๋“ค์–ด์ฃผ๊ฒ ์Šต๋‹ˆ๋‹ค.

std::string FindValue(int n)
{
    std::string result = "1";
    for (int i = 0; i < n; i++) {  // n๋ฒˆ ์ œ๊ณฑํ•ด์ฃผ์–ด์•ผ ํ•จ.
        // 
    }
    result[result.size()-1]--;  // ์ œ๊ณฑ ํ›„ -1์„ ํ•ด์ค˜์•ผํ•จ.
    return result;
}

์ด ๋•Œ ๋งˆ์ง€๋ง‰ ์ฝ”๋“œ์ธ ์ œ๊ณฑ ํ›„ result์— -1 ํ•ด์ฃผ๋Š” ์—ฐ์‚ฐ์€ ํ•ด๋ณด๋ฉด ์•Œ๊ฒ ์ง€๋งŒ ์ผ์˜์ž๋ฆฌ ์ˆซ์ž๊ฐ€ 2, 4, 8, 6 ์œผ๋กœ ์ˆœํ™˜ํ•˜๋ฏ€๋กœ 0์ด ๋‚˜์˜ค์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— 10์˜ ์ž๋ฆฌ ์ˆซ์ž๋ฅผ ๋ถˆ๋Ÿฌ์˜ฌ ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ €๋ ‡๊ฒŒ -1๋งŒ ์—ฐ์‚ฐ ํ•ด์ฃผ์–ด๋„ ๋ฌธ์ œ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.

๋ณดํ†ต ๊ณฑ์…ˆ์€ ์ขŒ์ธก๋ถ€ํ„ฐ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ฌธ์ž์—ด ๋งจ ์ขŒ์ธก๋ถ€ํ„ฐ ์šฐ์ธก์œผ๋กœ ๊ฐ€๋ฉด์„œ ์—ฐ์‚ฐํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.
์šฐ๋ฆฌ๋Š” 2๋ฅผ ์ œ๊ณฑํ•˜๋Š” ์—ฐ์‚ฐ์ด๋ฏ€๋กœ '0'์„ ๋บ๋‹ค๊ฐ€ 2๋ฅผ ๊ณฑํ•˜๊ณ  ๋‹ค์‹œ 0์„ ๋”ํ•˜๋Š” ํ˜•์‹์œผ๋กœ ascii์ฝ”๋“œ๋ฅผ ์ˆซ์ž๋กœ ์‚ฌ์šฉํ•˜์—ฌ ๋ด…์‹œ๋‹ค.
์ด๋ฅผ ์ฝ”๋“œ๋กœ ๊ตฌํ˜„ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

for (int j = 0; j < result.size(); j++) {  // ๋ฌธ์ž์—ด์˜ ์ขŒ์ธก๋ถ€ํ„ฐ
    result[j] = (result[j] - '0') * 2 + '0';  // ์ž„์‹œ๋กœ `0`์„ ๋บ€ ํ›„ 2๋ฅผ ๊ณฑํ•œ ํ›„ ๋‹ค์‹œ `0`์„ ๋”ํ•จ.
}

ํ•˜์ง€๋งŒ result[j]๊ฐ€ '9'๊ฐ€ ๋„˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ๊ฒ ์ฃ . ์˜ˆ๋ฅผ ๋“ค์–ด 7*2 = 14 ์ธ ๊ฒฝ์šฐ๋Š” ์ € ์—ฐ์‚ฐ์„ ํ•˜๋ฉด result[j]๋Š” ascii์ฝ”๋“œ๋กœ 62์˜ ๊ฐ’์„ ๊ฐ€์ง€๋Š” '>'๊ฐ€ ์ €์žฅ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ด๋Ÿฐ ๊ฒฝ์šฐ๋Š” ๋”ฐ๋กœ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
์ผ๋ฐ˜์ ์œผ๋กœ ์šฐ๋ฆฌ๋Š” ์ด๋Ÿฐ ๊ฒฝ์šฐ ๋‹ค์Œ ์ž๋ฆฌ ์ˆซ์ž์— 1์„ ๋”ํ•ด์ฃผ๊ณ , -10์„ ํ•ด์ฃผ๋Š” ๊ฒƒ์ด ์ผ๋ฐ˜์ ์ด์ฃ .
์ฝ”๋“œ๋กœ ํ‘œํ˜„ํ•˜๋ฉด result[j] -= 10 ํ•ด์ฃผ๊ณ , result[j-1]++ ํ•ด์ฃผ๋ฉด ๋˜๊ฒ ์ง€์š”.

ํ•˜์ง€๋งŒ ์˜ˆ์™ธ์ ์œผ๋กœ ์ตœ๋Œ€์ž๋ฆฟ์ˆ˜ ์ˆซ์ž๊ฐ€ 10์„ ๋„˜๊ฒŒ ๋œ๋‹ค๋ฉด ์–ด๋–ป๊ฒŒ ๋ ๊นŒ์š”? (j == 0 ์ด๋ผ๋ฉด?)
์ด ๋•Œ๋Š” ๋‹ค์Œ ์ž๋ฆฌ ์ˆซ์ž๊ฐ€ ์—†์œผ๋ฏ€๋กœ ๋ฌธ์ž์—ด ๋งจ ์•ž์— push๋ฅผ ํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.
์ด๋ฅผ ์ฝ”๋“œ๋กœ ๊ตฌํ˜„ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

result[j] -= 10;
if (j == 0) result.insert(result.begin(), '1'), j++;
else result[j-1]++;

PR์“ฐ๋ฉด์„œ ๊นจ๋‹ฌ์€ ๊ฑด๋ฐ ์ถ”๊ฐ€์ ์œผ๋กœ result[j-1]++ ํ•  ๋•Œ result[j-1]๋„ '9'๋ฅผ ๋„˜๊ธธ ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ์–ด์„œ ์ด ๋•Œ๋„ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ทผ๋ฐ ์ฒ˜๋ฆฌ๋ฅผ ์•ˆ ํ•ด๋„ ๋งž๋‹ค๊ณ  ๋œจ๋„ค์š”. (์ด๊ฒŒ ์™œ ๋จ???) ์•„๋งˆ 2์ œ๊ณฑ๋งŒ ํ•˜๋Š” ํŠน์ˆ˜ํ•œ ์ผ€์ด์Šค ๋•Œ๋ฌธ์— ๊ทธ๋Ÿฐ ๊ฒƒ ๊ฐ™๋„ค์š”..
์•„๋ฌดํŠผ ์ƒ๊ฐ๋‚œ ๊น€์— ์ด๋ฅผ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ค์‹œ๋‹ค. ์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์งค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

result[j] -= 10;
if (j == 0) result.insert(result.begin(), '1'), j++;  // ๋งŒ์•ฝ result[j]๊ฐ€ ๋งจ ์•ž ๋ฌธ์ž๋ผ๋ฉด '1'์„ ๋งจ ์•ž์ชฝ์— push
else {
    for (int k = 1; k <= j; k++) {  // j-1๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜์—ฌ ๋ฌธ์ž์—ด ์ขŒ์ธก์œผ๋กœ ์ด๋™ํ•˜๋ฉฐ result์˜ ๊ฐ ๋ฌธ์ž ํ™•์ธ
        if (result[j-k] == '9') result[j-k] = '0';  // ๋ฌธ์ž๊ฐ€ '9'๊ฐ€ ๋‚˜์˜ค๋ฉด '0'์œผ๋กœ ๋ฐ”๊ฟˆ
        else {                                                  // ๋ฌธ์ž๊ฐ€ '9'๊ฐ€ ์•„๋‹ˆ๋ฉด '9'๋ณด๋‹ค ์ž‘๋‹ค๋Š” ๋œป์ด๋ฏ€๋กœ 1 ๋”ํ•ด์ฃผ๊ณ  break;
            result[j-k]++;
            break;
        }
    }
    if (result[0] == '0') result.insert(result.begin(), '1'), j++;  // ๋งŒ์•ฝ ์ตœ๋Œ€์ž๋ฆฟ์ˆ˜ ๋ฌธ์ž๊นŒ์ง€ '9'์˜€์—ˆ๋‹ค๋ฉด ๋งจ ์•ž์— '1' push
}

3. ๋น ๋ฅธ ์ž…์ถœ๋ ฅ in C++

ํ˜น์‹œ ๋ชฐ๋ผ์„œ ์ฒ˜์Œ ๋ฌธ์ œ๋ฅผ ๋งž์ท„์—ˆ์„ ๋•Œ ๋น ๋ฅธ ์ž…์ถœ๋ ฅ๋„ ์ถ”๊ฐ€ํ•ด ์ฃผ์—ˆ์—ˆ์Šต๋‹ˆ๋‹ค.

std::ios_base::sync_with_stdio(false);  // stdio.h ๋ฒ„ํผ์™€ ๋™๊ธฐํ™” ํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ์จ cin, cout ๊ฐ€์†
std::cin.tie(NULL);  // ์ถœ๋ ฅ์„ ๋งจ ๋งˆ์ง€๋ง‰์— ํ•œ๊บผ๋ฒˆ์— ํ•ด์คŒ์œผ๋กœ์จ ์‹œ๊ฐ„์„ ๋‹จ์ถ•์‹œํ‚ด.

์‚ฌ์‹ค cin๊ณผ cout์„ ์“ฐ์ง€ ์•Š๊ณ , sacnf์™€ printf๋ฅผ ์“ฐ๋Š” ๊ฒƒ์ด ๋นจ๋ผ์„œ ์ด ๋‘ ๊ฐœ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๊ทธ๋งŒํผ ์‹œ๊ฐ„์ด ๋‹จ์ถ•๋œ๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.
์ž…์ถœ๋ ฅ ๋ฌธ์ œ ๊ด€ํ•ด์„œ๋Š” ์•„๋ž˜์ชฝ ์ฐธ๊ณ ํ•˜์‹œ๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
์‚ฌ์‹ค ์ƒ๊ฐํ•ด๋ณด๋‹ˆ ํ•จ์ˆ˜ ์•ˆ์—์„œ๋งŒ ํ‚ฌ ์ˆ˜ ์žˆ์–ด์„œ ์ด ์ฝ”๋“œ์—์„œ๋Š” ํ•˜๋‚˜, ์•ˆ ํ•˜๋‚˜ ์ฐจ์ด๊ฐ€ ๋ณ„๋กœ ์—†๋Š” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
(๋น„๊ตํ•ด๋ณด๋‹ˆ ๋Œ€์ถฉ 30ms ์ฐจ์ด๋‚˜๋„ค์š”.)

3. ์ตœ์ข… ์ฝ”๋“œ

#include <iostream>

std::string FindValue(int n)
{
    std::string result = "1";
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < result.size(); j++) {
            result[j] = (result[j] - '0') * 2 + '0';
            if (result[j] > '9') {
                result[j] -= 10;
                if (j == 0) result.insert(result.begin(), '1'), j++;
                else result[j-1]++;
            }
        }
    }
    result[result.size()-1]--;
    return result;
}

void Hanoi(int level, int start, int between, int end)
{
    if (level == 1) std::cout << start << " " << end << "\n";
    else {
        Hanoi(level-1, start, end, between);
        std::cout << start << " " << end << "\n";
        Hanoi(level-1, between, start, end);
    }
    return;
}

int main(void) {
    int n;
    std::cin >> n;
    std::cout << FindValue(n) << "\n";
    if (!(n > 20)) Hanoi(n, 1, 2, 3);
    return 0;
}

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

์ตœ๊ทผ์— ์žฌ๊ท€๋ฅผ ๋งŽ์ด ์—ฐ์Šตํ•ด๋ณด๋‹ˆ ์ „์— ๋ชปํ–ˆ๋˜ ํ•˜๋…ธ์ด ํƒ‘ ๋ฌธ์ œ๋„ ์ˆ˜์›”ํ•˜๊ฒŒ ๊ฐ€๋Šฅํ•˜๋„ค์š”.
์˜คํžˆ๋ ค ๋น ๋ฅธ ์ž…์ถœ๋ ฅ๊ณผ ์ž„์˜ ์ •๋ฐ€๋„ / ํฐ ์ˆ˜ ์—ฐ์‚ฐ ์ธก๋ฉด์—์„œ ์‹œ๊ฐ„์„ ๋” ๋งŽ์ด ์ป์Šต๋‹ˆ๋‹ค.
C++์—์„œ๋Š” ์ด๊ฑธ ์ง์ ‘ ๊ตฌํ˜„ํ•˜๋‹ค ๋ณด๋‹ˆ ์ข€ ์–ด๋ ต๋„ค์š”.. (์ด ๋•Œ ๋งŒํผ์€ ํŒŒ์ด์ฌ์ด ๋ถ€๋Ÿฝ์Šต๋‹ˆ๋‹ค..)
์ฐธ๊ณ ๋กœ ์ด ๋ฌธ์ œ๋ฅผ ํ’€๋ฉด ์ „์— PR์— ์˜ฌ๋ ค์ฃผ์…จ๋˜ ํ•˜๋…ธ์ด ํƒ‘ ์ด๋™์ˆœ์„œ ๋ฌธ์ œ๋„ 5๋ถ„ ์ด๋‚ด๋กœ ์ˆ˜์ •ํ•ด์„œ ํ’€ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์˜ค๋žœ๋งŒ์— ๊ตฌ๊ธ€๋ง ๊ณ„์† ํ•ด๊ฐ€๋ฉด์„œ ํ…Œ์ŠคํŠธ ์ฝ”๋“œ ๊ณ„์† ์งœ๊ณ , ์‹คํ—˜ํ•ด๋ณด๊ณ  ์—ฌ๋Ÿฌ๋ชจ๋กœ ์žฌ๋ฐŒ์—ˆ๋˜ ๋ฌธ์ œ์˜€์Šต๋‹ˆ๋‹ค.
์•„๋ž˜๋Š” ์ € ๋‘ ๋ฌธ์ œ ํ’€๋ฉด์„œ ์ž…์ถœ๋ ฅ ๋ฌธ์ œ ๊ด€ํ•ด์„œ ์ฐธ๊ณ ํ–ˆ๋˜ ๋ธ”๋กœ๊ทธ๋“ค์ž…๋‹ˆ๋‹ค.
C++ ์ž…์ถœ๋ ฅ ๋น ๋ฅด๊ฒŒ ํ•˜๊ธฐ (1)
C++ ์ž…์ถœ๋ ฅ ๋น ๋ฅด๊ฒŒ ํ•˜๊ธฐ (2)
C++ ์ž…์ถœ๋ ฅ ๋น ๋ฅด๊ฒŒ ํ•˜๊ธฐ (3)

Copy link
Member

@kjs254 kjs254 left a comment

Choose a reason for hiding this comment

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

์ต์ˆ™ํ•œ ๋ฌธ์ œ๋ฅผ ์ƒˆ๋กญ๊ฒŒ ๋‹ค์‹œ ๋ณด๊ฒŒ๋˜๋„ค์š”. ์žฌ๊ท€ ๋ถ€๋ถ„์€ ์ €๋ฒˆ์— ๋ฏผ์„ฑ๋‹˜์ด ์„ค๋ช…ํ•˜์…”์„œ ๊ธˆ๋ฐฉ ์ดํ•ด๊ฐ€ ๋˜์—ˆ์ง€๋งŒ N์ด ์ปค์ง€๋Š” ํฌ๋Š” ๊ฒฝ์šฐ๋Š” ๊นŠ๊ฒŒ ์ƒ๊ฐํ•ด๋ณด์ง€ ์•Š์•˜๋„ค์š”.

์žฌ๊ท€๋ณด๋‹ค ์ด ๊ฒฝ์šฐ๋ฅผ ๊ตฌํ˜„ํ•˜๋Š”๊ฒŒ ๋” ์–ด๋ ค์›Œ๋ณด์ด๋Š” ๋ฌธ์ œ๋„ค์š”..

๋˜ํ•œ ์‹คํ–‰ ํšŸ์ˆ˜๋Š” ์ ํ™”์‹์„ ํ†ตํ•ด ์ผ๋ฐ˜์‹์„ ๊ตฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ผ๋ฐ˜์‹์„ ์ผ์ผ์ด ์จ๋‚ด๋ ค๊ฐ€๋ฉด์„œ ๊ตฌํ•˜๋Š”๊ฑด ๊ฝค ๊ท€์ฐฎ์•„์„œ ๋ช‡ ๊ฐœ์˜ ์ˆซ์ž๋ฅผ ๋„ฃ์–ด์„œ ์œ ์ถ”ํ•˜๋Š”๊ฒŒ ์‚ฌ์‹ค ๋” ๋น ๋ฅธ ๊ฒƒ์ด ์‚ฌ์‹ค์ž…๋‹ˆ๋‹ค.

PR์“ฐ๋ฉด์„œ ๊นจ๋‹ฌ์€ ๊ฑด๋ฐ ์ถ”๊ฐ€์ ์œผ๋กœ result[j-1]++ ํ•  ๋•Œ result[j-1]๋„ '9'๋ฅผ ๋„˜๊ธธ ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ์–ด์„œ ์ด ๋•Œ๋„ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ทผ๋ฐ ์ฒ˜๋ฆฌ๋ฅผ ์•ˆ ํ•ด๋„ ๋งž๋‹ค๊ณ  ๋œจ๋„ค์š”. (์ด๊ฒŒ ์™œ ๋จ???) ์•„๋งˆ 2์ œ๊ณฑ๋งŒ ํ•˜๋Š” ํŠน์ˆ˜ํ•œ ์ผ€์ด์Šค ๋•Œ๋ฌธ์— ๊ทธ๋Ÿฐ ๊ฒƒ ๊ฐ™๋„ค์š”..

๋ง์”€ํ•˜์‹ ๋Œ€๋กœ 2์ œ๊ณฑ ์ผ€์ด์Šค๋ผ ์ผ์˜ ์ž๋ฆฟ์ˆ˜๊ฐ€ 2, 4, 8, 6, ๋งŒ ๋‚˜ํƒ€๋‚˜๊ธฐ ๋•Œ๋ฌธ์— 9๋ฅผ ๋„˜์ง€ ์•Š๋Š”๋‹ค๊ณ  ์ƒ๊ฐ๋˜๋„ค์š”.

Copy link
Collaborator

@mong3125 mong3125 left a comment

Choose a reason for hiding this comment

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

C++์—์„œ๋Š” ํฐ ์ˆ˜๋ฅผ ํ‘œํ˜„ํ• ๋•Œ ๋ถ€๋‹ด์ด ์ƒ๊ธฐ๋Š”๊ตฐ์š”...
๋ฌธ์ž์—ด๋กœ ์ˆซ์ž๋ฅผ ํ‘œํ˜„ํ•˜๊ณ  ๊ณ„์‚ฐํ•˜๋Š”๊ฒŒ ๋†€๋ž๋„ค์š”

java์—์„œ๋Š” BigInteger๋ผ๋Š”๊ฒŒ ์žˆ์–ด์„œ ๊ทธ๋Ÿฐ ๋ฌธ์ œ๋Š” ์—†์—ˆ๋Š”๋ฐ ๋‹ค๋ฅธ ๋ฌธ์ œ๊ฐ€ ์ƒ๊ฒผ์—ˆ์Šต๋‹ˆ๋‹ค.

์ถœ๋ ฅํ• ๋•Œ ๋ฐ”๋กœ print๋กœ ์ถœ๋ ฅํ•˜๋ฉด ๋ฉ”๋ชจ๋ฆฌ ์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ•˜๊ณ  list์— ์ €์žฅํ•œ๋‹ค์Œ ๋ชจ์•„์„œ ์ถœ๋ ฅํ•˜๋ฉด ๋ฉ”๋ชจ๋ฆฌ ์ดˆ๊ณผ์—†์ด ํ†ต๊ณผํ•˜๋”๊ตฐ์š”..

์ด ๋ฌธ์ œ๋กœ ํ•œ์ฐธ ํ•ด๋งธ๋Š”๋ฐ ์•„์ง ์ž˜ ๋ชจ๋ฅด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ฃผ๋ณ€์‚ฌ๋žŒ์ด๋ž‘ ๊ฐ™์ด ๋…ผ์˜ ํ•ด๋ณธ๊ฒฐ๊ณผ ๋ฐฑ์ค€ ํ™˜๊ฒฝ๋ฌธ์ œ์ง€ ์•Š์„๊นŒ ํ•˜๋Š” ์˜์‹ฌ์„ ํ•ด๋ณด๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค ใ…‹ใ…‹ ๋งž์™œํ‹€

System.out.printf("%d %d\n", from, to); ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ํ‹€๋ฆฌ๊ณ 
steps.add(new int[] {from, to}); ์ด๋ ‡๊ฒŒ ํ•˜๊ณ  ๋‚˜์ค‘์— for ๋ฌธ ๋Œ๋ ค์„œ ์ถœ๋ ฅํ•˜๋ฉด ํ†ต๊ณผํ•ฉ๋‹ˆ๋‹ค.

Copy link
Collaborator

@rivkms rivkms left a comment

Choose a reason for hiding this comment

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

ํ•˜๋…ธ์ดํƒ‘ ๋ฌธ์ œ๊ฐ€ ํ™•์‹คํžˆ ์žฌ๊ท€๋ฅผ ์ตํžˆ๊ธฐ์—๋Š” ์ข‹์€ ๋ฌธ์ œ์ธ ๊ฒƒ ๊ฐ™๋„ค์š”!!

๋น ๋ฅธ ์ž…์ถœ๋ ฅ๋„ ์‚ฌ์šฉํ•ด์ฃผ์…จ๋Š”๋ฐ ์ƒ๊ฐ๋ณด๋‹ค ์ € ๋น ๋ฅธ ์ž…์ถœ๋ ฅ์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•˜์„ ๋•Œ ์‹œ๊ฐ„์ดˆ๊ณผ๋‚˜๋Š” ๋ฌธ์ œ๊ฐ€ ์žˆ๋Š” ๊ฒƒ๋„ ๊ฐ™์•„์„œ ์ €๋„ ์ €๊ฑธ ์“ฐ๊ณ  ์‹œ์ž‘ํ•˜๋Š”๊ฑธ ์Šต๊ด€ํ™”ํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค

์ถœ๋ ฅ์„ ํ•˜์‹ค ๋•Œ ๊ทธ ์ ํ™”์‹์„ ์ฐพ๊ณ , ์ด๋ฅผ ์ถœ๋ ฅํ•จ์œผ๋กœ์„œ ์‹œ๊ฐ„์„ ๋‹จ์ถ•ํ•˜์‹  ๊ฒƒ๋„ ์ข‹์€ ์ฝ”๋“œ๋ผ๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค!!

์ˆ˜๊ณ ํ•˜์…จ์Šต๋‹ˆ๋‹ค๐Ÿ˜

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