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

2-rivkms #8

Merged
merged 1 commit into from
Feb 18, 2024
Merged

2-rivkms #8

merged 1 commit into from
Feb 18, 2024

Conversation

rivkms
Copy link
Collaborator

@rivkms rivkms commented Feb 15, 2024

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

ํ•˜๋…ธ์ด ํƒ‘ ์ด๋™ ์ˆœ์„œ

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

1h

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

โ–ถ๏ธ ๋ฌธ์ œ์˜ ์ข…๋ฅ˜

: Recursion(์žฌ๊ท€) : ์ž๊ธฐ ์ž์‹ ์„ ์ด์šฉํ•˜์—ฌ ์ •์˜ํ•˜๊ฑฐ๋‚˜ ์‘์šฉํ•˜๋Š” ๊ฒƒ. [์ •๋ณดํ†ต์‹ ๊ธฐ์ˆ ์šฉ์–ดํ•ด์„ค]

โ“์ด์œ 

๋ฌธ์ œ ์ข…๋ฅ˜์—์„œ Recursion์„ ์“ฐ๋ž˜์š”
ํƒ‘์„ ์˜ฎ๊ธฐ๋Š” ๊ณผ์ •์ด ๋ฐ˜๋ณต๋˜๊ณ , ๊ทธ ๊ณผ์ •์ด ๋™์ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— Recursion๋ฅผ ์“ฐ๊ณ  ์‹ถ๋‹ค๊ณ  ํŒ๋‹จ

โ“์–ด๋–ค ๋ฐ˜๋ณต์ด ์žˆ๋Š”๊ฐ€

โ–ถ๏ธ ๋ฐ˜๋ณต ๊ณผ์ •์„ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•ด ์งง๊ฒŒ ๊ทธ๋ ค๋ณธ ๊ทธ๋ฆผ

ํƒ‘ ์ข…๋ฅ˜ ๋ช…์นญ
์›ํŒ์„ ์˜ฎ๊ธฐ๊ธฐ ์‹œ์ž‘ํ•˜๋Š” ํƒ‘ Start
์ค‘๊ฐ„์— ๊ฑฐ์ณ๊ฐ€๋Š” ํƒ‘ Center
์›ํŒ์ด ๋„์ฐฉํ•˜๋Š” ํƒ‘ End

๋ผ๊ณ  ํ•ฉ์‹œ๋‹ค.

โ™ฆ๏ธ N๊ฐœ์˜ ์›ํŒ์„ Start์—์„œ End๋กœ ์˜ฎ๊ธฐ๊ณ  ์‹ถ์Œ

  1. Start์—์„œ ์‹œ์ž‘ํ•ด์„œ Center์— N-1๊ฐœ์˜ ์›ํŒ์„ ๊ฐ€์ง„ ํƒ‘์„ ๋งŒ๋“ฌ
  2. End์— N๋ฒˆ์งธ ์›ํŒ์„ ์˜ฎ๊น€
  3. Center์—์„œ ์‹œ์ž‘ํ•ด์„œ End์— N-1๊ฐœ์˜ ์›ํŒ์„ ๊ฐ€์ง„ ํƒ‘์„ ๋งŒ๋“ฌ

N-1๊ฐœ์˜ ์›ํŒ์„ ์ด์šฉํ•˜์—ฌ ํƒ‘์„ ์˜ฎ๊ธฐ๋Š” ๊ฒƒ๋„ ์œ„์™€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค.
์ด ๋•Œ N๋ฒˆ์งธ๋ฅผ ํ’€๊ธฐ ์œ„ํ•œ ํ•จ์ˆ˜์—์„œ N-1๋ฒˆ์งธ์— ๋Œ€ํ•˜์—ฌ ๊ฐ™์€ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ํ’€์ดํ•  ์ˆ˜ ์žˆ๋„ค์š”?

โ€ผ๏ธ ์šฐ์™€ Recursion์„ ์“ฐ๋ฉด ๋˜๊ฒ ๋„ค

โ“ํƒˆ์ถœ์กฐ๊ฑด

โ€ผ๏ธ N์ด 1์ด๋ฉด Start์—์„œ End๋กœ ์˜ฎ๊ธฐ๋ฉด ๋˜๋Š”๊ตฌ๋‚˜

โœ”๏ธSudo code

1๏ธโƒฃ n์„ ์ž…๋ ฅ๋ฐ›์Œ
2๏ธโƒฃ n์„ ์ธ์ž๋กœ ํ•˜์—ฌ ํ•จ์ˆ˜ ํ˜ธ์ถœ

----ํ•จ์ˆ˜----
1๏ธโƒฃ center์— n-1๋ฒˆ์งธ ํƒ‘์ด ์™„์„ฑ(ํ•จ์ˆ˜ ํ˜ธ์ถœ(์žฌ๊ท€))
2๏ธโƒฃ ๋งจ ๋งˆ์ง€๋ง‰ ์กฐ๊ฐ์ด end๋กœ ์˜ฎ๊ธฐ๊ณ 
3๏ธโƒฃ center๋ฅผ ์‹œ์ž‘์œผ๋กœ ํ•˜์—ฌ end๋กœ ๋๋‚˜๋Š” n-1 ํƒ‘ ์™„์„ฑ
4๏ธโƒฃ ๊ฐ ๋™์ž‘์„ box์— ์ €์žฅํ•จ
--ํ•จ์ˆ˜ ๋--

3๏ธโƒฃ box์˜ ์›์†Œ ๊ฐœ์ˆ˜๋ฅผ ์ถœ๋ ฅ
4๏ธโƒฃ box ๋‚ด์šฉ์„ ์ถœ๋ ฅ

์ฝ”๋“œ

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

vector<pair<int, int>> box;

void func(const int & start, const int & center, const int & end, const int & n){
    if(n==1){
        box.push_back(pair<int, int> (start, end));
        return;
    }
    func(start, end, center, n-1);
    box.push_back(pair<int, int> (start, end));
    func(center, start, end, n-1);

    return;
}

int main(){
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    int n, a = 0; 
    cin >> n;
    func(1,2,3,n);
    cout << box.size() << "\n";
    for(pair<int, int> i : box){
        cout << i.first << " " << i.second << "\n";
    }

    return 0;
}

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

โœ”๏ธ ์žฌ๊ท€๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ค‘์—์„œ๋„ ๊ฐ„๋‹จํ•œ ๊ฒƒ์€ ๊ฐ„๋‹จํ•œ ํŽธ์— ์†ํ•˜๊ธด ํ•˜์ฃ ...?
๋Œ€์‹  ๋‹ค๋ฅธ ์ข‹์€ ๋ฐฉ๋ฒ•๋„ ์žˆ์„ ์ˆ˜ ์žˆ์œผ๋‹ˆ ๊ทธ ๋ฐฉ๋ฒ•๋„ ์‚ฌ์šฉํ•ด๋ณด๋Š” ๊ฒƒ์„ ์ถ”์ฒœํ•ฉ๋‹ˆ๋‹ค!!

โœ”๏ธ ์žฌ๊ท€๋ฅผ ์“ธ ๋•Œ์˜ ์ค‘์š”ํ•œ ์  2๊ฐ€์ง€โ€ผ๏ธ

 1๏ธโƒฃ ์–ด๋””์—์„œ ์žฌ๊ท€๋ฅผ ์‚ฌ์šฉํ•˜์ง€?
 2๏ธโƒฃ โœจ์žฌ๊ท€๋ฅผ ํƒˆ์ถœํ•  ์กฐ๊ฑดโœจ์€ ๋ฌด์—‡์ด ์žˆ์ง€?

ํƒˆ์ถœ ์กฐ๊ฑด์ด ์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์œผ๋ฉด ๋ฌดํ•œ๋ฐ˜๋ณตํ•˜๊ธฐ์—,...์ž˜ ์ ์–ด์•ผ๊ฒ ์ฃ ...
โ–ถ๏ธ ์ œ์ผ ์œ„์— ์กฐ๊ฑด์„ ์ž˜ ์ ์–ด์ฃผ๋Š”๊ฑธ ์Šต๊ด€ํ™” ํ•ฉ์‹œ๋‹ค..ใ…Žใ…Ž

โœ”๏ธ ๊ฐ„๋‹จํ•œ ๋ฌธ์ œ์ธ๊ฑฐ ๊ฐ™์€๋ฐ...์ด ๋ฌธ์ œ๊ฐ€ ๊ณจ๋“œ 5?
์ƒ๊ฐ๋ณด๋‹ค ์ € ๊ทœ์น™ ์ฐพ๊ธฐ๊ฐ€ ์–ด๋ ค์›Œ์„œ ๊ทธ๋Ÿฐ๊ฐ€์š”...? ๋‹ค๋ฅธ ๋ถ„๋“ค์˜ ๊ด€์ ๋„ ๋“ค์–ด๋ณด๊ณ  ์‹ถ์Šต๋‹ˆ๋‹คโ€ผ๏ธ

Copy link
Collaborator

@YIM2UL2ET YIM2UL2ET left a comment

Choose a reason for hiding this comment

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

์˜ˆ์ „์— ์žฌ๊ท€ ๋ฌธ์ œ๋ฅผ ์‹œ๋„ํ•ด ๋ณด์•˜๋‹ค๊ฐ€ ๋„๋ง์นœ ์ œ๊ฐ€ ๊ธฐ์–ต๋‚˜๋„ค์š”. ๊ฐœ์ธ์ ์„ ์žฌ๊ท€์˜ ๋‚œ์ด๋„๊ฐ€ ๋†’์€ ์ด์œ ๋Š” ๊ทœ์น™์„ ์ฐพ๊ธฐ๊ฐ€ ์‰ฝ์ง€ ์•Š๊ณ , ๊ทธ๊ฒƒ์„ ๊ตฌํ˜„ํ•˜๊ธฐ๋Š” ๊ฝค ์–ด๋ ต๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ž˜๋„ ์ง€๊ธˆ ์ฐจ๋ถ„ํ•˜๊ฒŒ ๋ณด๋‹ˆ๊น ์—ฐ์Šต ์กฐ๊ธˆ๋งŒ ํ•˜๋ฉด ๊ดœ์ฐฎ๊ฒ ๋‹ค๋Š” ์ƒ๊ฐ์ด ๋“œ๋„ค์š”. ํŠนํžˆ ํƒˆ์ถœ ์กฐ๊ฑด์ด ์ค‘์š”ํ•˜๋‹ค๋Š” ๊ฒƒ์„ ๊นจ๋‹ฌ์œผ๋‹ˆ ๋ˆˆ์ด ํ™• ํŠธ์ž…๋‹ˆ๋‹ค. ๋•๋ถ„์— ์ข‹์€ ์•„์ด๋””์–ด ์–ป์–ด๊ฐ‘๋‹ˆ๋‹ค. ์ˆ˜๊ณ ํ•˜์…จ์Šต๋‹ˆ๋‹ค.

Comment on lines +25 to +27
for(pair<int, int> i : box){
cout << i.first << " " << i.second << "\n";
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

์ € for๋ฌธ์€ ์–ด๋–ป๊ฒŒ ์ž‘๋™ํ•˜๋Š” ๊ฑด๊ฐ€์š”? ์ €๋Ÿฐ ๋ฐฉ๋ฒ•์€ ์ฒ˜์Œ ๋ณด๋„ค์š”.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

for-each ๋ฌธ์ด๋ผ๊ณ  ํ•˜๋Š” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
vector๋‚˜ string ๋“ฑ์—์„œ ๊ฐ ์š”์†Œ์— ์ ‘๊ทผํ•  ๋•Œ ์ด ๋ฐฉ๋ฒ•์œผ๋กœ ์ ‘๊ทผํ•˜๋ฉด ํŽธํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์–ด์š”

Java, Python ๋“ฑ์—์„œ๋„ ์‚ฌ์šฉ๋˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ˆ ์ฐธ๊ณ ํ•ด๋ณด์‹œ๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

์ถ”๊ฐ€ ์ฐธ๊ณ ์ž๋ฃŒ : for-each ๋ฐ˜๋ณต๋ฌธ

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.

์ˆ˜์—ด๊ณผ ์ ํ™”์‹, ์žฌ๊ท€ํ•จ์ˆ˜์— ๋‹จ๊ณจ๋กœ ๋“ฑ์žฅํ•˜๋Š” ํ•˜๋…ธ์ดํƒ‘ ๋ฌธ์ œ๋„ค์š”.

Recursion์€ ์ง๊ด€์ ์œผ๋กœ ์ดํ•ดํ•˜๊ธฐ๋Š” ์‰ฝ์ง€๋งŒ ์‹ค์ œ๋กœ ๊ตฌํ˜„ํ•˜๋Š”๋ฐ ๊ฝค ์–ด๋ ต๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค.

ํŠนํžˆ ๊ฐ•์กฐํ•˜์‹  ์ดˆ๊ธฐ ์กฐ๊ฑด๊ณผ ํ•จ์ˆ˜๋ฅผ ์–ด๋””์„œ ๋‹ค์‹œ ํ˜ธ์ถœํ•ด์•ผ ํ•˜๋Š”์ง€ ์ € ๊ฐ™์€ ์ดˆ๋ณด์ž๋Š” ๋งŽ์ด ํ—ท๊ฐˆ๋ฆฌ๊ณค ํ•ฉ๋‹ˆ๋‹ค.

์ข‹์€ ๋…ธํ•˜์šฐ ๋ฐฐ์šฐ๊ณ ๊ฐ‘๋‹ˆ๋‹ค.

Comment on lines +7 to +17
void func(const int & start, const int & center, const int & end, const int & n){
if(n==1){
box.push_back(pair<int, int> (start, end));
return;
}
func(start, end, center, n-1);
box.push_back(pair<int, int> (start, end));
func(center, start, end, n-1);

return;
}
Copy link
Member

@kjs254 kjs254 Feb 18, 2024

Choose a reason for hiding this comment

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

n=1 ์ผ ๋•Œ start end ๋ฅผ ์ดˆ๊ธฐ ์กฐ๊ฑด์œผ๋กœ ์„ธํŒ…ํ•˜๊ณ  start center end ์œ„์น˜๋ฅผ ๋ณ€๊ฒฝํ•˜์—ฌ ์žฌ๊ท€ ํ•จ์ˆ˜๋ฅผ ์„ ์–ธํ•œ ์ ์ด ๋„ˆ๋ฌด ๊น”๋”ํ•ด์„œ ๋ณด๊ธฐ ์ข‹์Šต๋‹ˆ๋‹ค.

n์ธต์˜ ํ•˜๋…ธ์ดํƒ‘์€ n-1์ธต์˜ ํ•˜๋…ธ์ดํƒ‘์„ 2๋ฒˆ ์˜ฎ๊ธฐ๊ณ , 1์ธต์˜ ํ•˜๋…ธ์ดํƒ‘์„ 1๋ฒˆ ์˜ฎ๊ธฐ๋Š” ์ ์„ ์ง๊ด€์ ์œผ๋กœ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋Š” ์ข‹์€ ์ฝ”๋“œ๋„ค์š” ๐Ÿ˜„

@rivkms rivkms merged commit 10fda93 into main Feb 18, 2024
1 check passed
@rivkms rivkms deleted the 2-rivkms branch February 18, 2024 11:28
@rivkms rivkms restored the 2-rivkms branch February 18, 2024 12:53
@rivkms rivkms deleted the 2-rivkms branch March 11, 2024 08:49
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.

3 participants