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

1-oesnuj #4

Merged
merged 3 commits into from
Mar 29, 2024
Merged

1-oesnuj #4

merged 3 commits into from
Mar 29, 2024

Conversation

oesnuj
Copy link
Member

@oesnuj oesnuj commented Mar 26, 2024

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

https://www.acmicpc.net/problem/1935

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

25๋ถ„

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

  1. ํ›„์œ„ ํ‘œ๊ธฐ์‹์„ ๋ฌธ์ž์—ด๋กœ ๋ฐ›๊ณ , ์•ŒํŒŒ๋ฒณ์— ์น˜ํ™˜๋˜๋Š” ๊ฐ’์„ ํฌ๊ธฐ๊ฐ€ 26์ธ ๋ฐฐ์—ด์— ์ˆœ์„œ๋Œ€๋กœ ์ €์žฅํ•œ๋‹ค.
    image

  2. ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋งŒํผ ์ฒซ๋ฒˆ์งธ ๋ฌธ์ž๋ถ€ํ„ฐ ํƒ์ƒ‰์„ ์‹œ์ž‘ํ•œ๋‹ค.

  • ํ•ด๋‹น๋ฌธ์ž๊ฐ€ ํ”ผ์—ฐ์‚ฐ์ž(A ~ Z)๋ผ๋ฉด ํฌ๊ธฐ 26์ธ ๋ฐฐ์—ด์˜ ์ธ๋ฑ์Šค[ํ•ด๋‹น ๋ฌธ์ž - 'A']์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ’์„ ์Šคํƒ์— Push
  • ํ•ด๋‹น๋ฌธ์ž๊ฐ€ ์—ฐ์‚ฐ์ž๋ผ๋ฉด ์Šคํƒ์—์„œ ํ”ผ์—ฐ์‚ฐ์ž 2๊ฐœ๋ฅผ ๋นผ์„œ(pop 2๋ฒˆ) ํ•ด๋‹น ์—ฐ์‚ฐ์ž๋กœ ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค. ๊ทธ ์—ฐ์‚ฐ์˜ ๊ฒฐ๊ณผ๊ฐ’์„ ๋‹ค์‹œ ์Šคํƒ์— Push

๐Ÿ’ก์ฃผ์˜ํ•  ์ !
์Šคํƒ์—์„œ ๋นผ์„œ ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ• ๋•Œ +, *๋Š” ํ”ผ์—ฐ์‚ฐ์ž์˜ ์ˆœ์„œ๊ฐ€ ์ƒ๊ด€์—†์ง€๋งŒ /, -๋Š” ์ˆœ์„œ๋ฅผ ์‹ ๊ฒฝ์จ์ฃผ์–ด์•ผํ•œ๋‹ค.

  1. ๋ฌธ์ž์—ด ์ „์ฒด๋ฅผ ๋Œ๋ฉด์„œ ์œ„ ๋กœ์ง ์ˆ˜ํ–‰

  2. Stack์˜ ๋‹จ ํ•˜๋‚˜ ๋‚จ์€ ๋งˆ์ง€๋ง‰ ๊ฐ’(์Šคํƒ์˜ Top)์ด ์—ฐ์‚ฐ ๊ฒฐ๊ณผ๊ฐ€ ๋œ๋‹ค.
    image

๐Ÿ“ƒ ์ฝ”๋“œ

#include <iostream>
#include <string>
#include <stack>
#include <iomanip>
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);   cin.tie(NULL);
    stack <double> s;

    int n;  
    cin >> n;
    string postfix;  
    cin >> postfix;
    double alphabet[26] = {0};
    for (int i = 0; i < n; i++) //ํ”ผ์—ฐ์‚ฐ์ž(์•ŒํŒŒ๋ฒณ)์— ๋Œ€์‘๋˜๋Š” ๊ฐ’ ์ €์žฅํ•ด๋†“๊ธฐ
        cin >> alphabet[i];

    for (int i = 0; i < postfix.length(); i++)
    {
        if (postfix[i] >= 'A' && postfix[i] <= 'Z') //ํ”ผ์—ฐ์‚ฐ์ž๋ผ๋ฉด ํ•ด๋‹น ๊ฐ’ push
            s.push(alphabet[postfix[i] - 'A']);

        else //์—ฐ์‚ฐ์ž๋ฅผ ๋งŒ๋‚œ๋‹ค๋ฉด ์Šคํƒ์˜ top 2๊ฐœ๋ฅผ ๊บผ๋‚ด์–ด ์—ฐ์‚ฐ ์ˆ˜ํ–‰ํ›„ ํ•ด๋‹น ๊ฐ’ push
        {
            double op1 = s.top();
            s.pop();
            double op2 = s.top();
            s.pop();
            if (postfix[i] == '+')
                s.push(op2 + op1);
            else if (postfix[i] == '-')
                s.push(op2 - op1);
            else if (postfix[i] == '*')
                s.push(op2 * op1);
            else if (postfix[i] == '/')
                s.push(op2 / op1);
        }
    }
    cout << fixed << setprecision(2) << s.top(); //๋งˆ์ง€๋ง‰ top์ด ๊ฒฐ๊ณผ๊ฐ’ / ์†Œ์ˆ˜์  ๋‘˜์งธ์งœ๋ฆฌ ๊นŒ์ง€ ์ถœ๋ ฅ
    return 0;
}

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

@Ghdrn1399
Copy link
Contributor

์Šคํƒ ๊ตฌํ˜„์›๋ฆฌ๋ฅผ ์žŠ๊ณ ์žˆ์—ˆ๋Š”๋ฐ ๋•๋ถ„์— ์ž˜ ์•Œ์•„๊ฐ‘๋‹ˆ๋‹ค!!

@suhyun113
Copy link
Collaborator

  • ์‚ฌ์‹ค ํ›„์œ„ ํ‘œ๊ธฐ์‹? ์ด๋ผ๋Š” ๊ฒƒ์„ ์ฒ˜์Œ ๋“ค์–ด๋ดค์Šต๋‹ˆ๋‹ค. ๋ฌธ์ œ๊ฐ€ ์ƒ์†Œํ•˜์˜€๋Š”๋ฐ, ์ด ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด์„œ ํ›„์œ„ ํ‘œ๊ธฐ์‹์ด ๋ฌด์—‡์ธ์ง€ ์•Œ๊ฒŒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

  • C++,,,์ œ๊ฐ€ ํ˜„์žฌ ๋ฐฐ์šฐ๋Š” ์ค‘์ธ ์–ธ์–ด์ž…๋‹ˆ๋‹ค. ์•„์ง ๊ทน ์ดˆ๋ฐ˜์„ ๋ฐฐ์šฐ๋Š” ์ค‘์ด๋ผ ๊ณผ ๋Š” ์ฒ˜์Œ ๋ณด๋Š” ํ—ค๋”๋“ค์ด๋ผ ์‹ ๊ธฐํ–ˆ๊ณ , ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ์–ป์–ด๊ฐ€๋Š” ๊ฒƒ ๊ฐ™์•„ ๋„์›€์ด ๋งŽ์ด ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

  • ์Šคํƒ ๊ตฌํ˜„์€ ์ •๋ง ๋ง›๋ณด๊ธฐ๋กœ python์œผ๋กœ๋งŒ ํ•ด๋ณด์•˜๋Š”๋ฐ, C++์—์„œ๋Š” ํ—ค๋”๋ฅผ ์ด์šฉํ•ด ์‚ฌ์šฉํ•œ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ๊ฒŒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

  • ์Šคํƒ์˜ ๊ฐœ๋…์ด ๊ธฐ์–ต๋‚˜์ง€ ์•Š์•˜๋Š”๋ฐ, ๊ทธ๋ฆผ์„ ํ†ตํ•ด ์„ค๋ช…ํ•ด์ฃผ๋‹ˆ ์ง๊ด€์ ์œผ๋กœ ์•Œ๊ธฐ ์‰ฌ์› ์Šต๋‹ˆ๋‹ค. ์Šคํƒ ์œ ํ˜•์˜ ๋ฌธ์ œ๋ฅผ ํ’€ ๋•Œ๋Š” ์–ด๋–ค ์‹์œผ๋กœ ๊ตฌํ˜„ํ•˜๋Š”์ง€ ์•Œ๊ฒŒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค!

์Šคํƒ์— ๋Œ€ํ•ด ์ž˜ ๋ชจ๋ฅด๋Š”๋ฐ, ์ฝ”๋“œ๋ฅผ ๋ณด๋‹ˆ ์ดํ•ด๊ฐ€ ์–ด๋Š์ •๋„ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค! ์ข‹์€ ์ฝ”๋“œ ์•Œ์•„๊ฐ€์š”! PR์ž‘์„ฑํ•œ๋‹ค๊ณ  ์ˆ˜๊ณ ๋งŽ์œผ์…จ์Šต๋‹ˆ๋‹ค~

Copy link
Collaborator

@pu2rile pu2rile left a comment

Choose a reason for hiding this comment

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

์˜ˆ์ „์— ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ๊ณต๋ถ€ํ•˜๋ฉด์„œ ์ž์ฃผ ๋ดค๋˜ ์Šคํƒ-ํ›„์œ„ํ‘œ๊ธฐ์‹์„ ์—ฌ๊ธฐ์„œ ๋‹ค์‹œ ๋ณด๋‹ˆ ๊ฐํšŒ๊ฐ€ ์ƒˆ๋กญ๋„ค์š” ใ…Žใ…Ž
์„ค๋ช…๋„ ์ž์„ธํžˆ ์ž˜ ์จ ์ฃผ์…จ์ง€๋งŒ ํ™•์‹คํžˆ ๊ทธ๋ฆผ ์ž๋ฃŒ๊ฐ€ ์žˆ์œผ๋‹ˆ ํ›จ์”ฌ ๋ณด๊ธฐ ์‰ฝ๊ณ  ์ดํ•ด๊ฐ€ ์ž˜ ๋˜๋Š” ๊ฒƒ ๊ฐ™์•„์š”!
์ €๋„ ๋‹ค์Œ pr์—์„œ๋Š” ๊ทธ๋ฆผ ์ž๋ฃŒ๋ฅผ ์ ๊ทน ํ™œ์šฉํ•ด ๋ด์•ผ๊ฒ ์Šต๋‹ˆ๋‹ท ์ฒซ pr ์ˆ˜๊ณ ํ•˜์…จ์–ด์š”!

@9kyo-hwang
Copy link

@honggukang0623 @suhyun113 ์—ฌ๋Ÿฌ๋ถ„ 'comment'๋กœ ๋‹ค๋Š” ๊ฒŒ ์•„๋‹ˆ๋ผ approve๋กœ ๋‹ฌ์•„์•ผ ๋ฆฌ๋ทฐ ์ฒดํฌ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค!

Copy link

@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.

๊น”๋”ํ•˜๊ฒŒ ์ฝ”๋“œ ์ž˜ ์งœ์‹  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค! ๋‹ค๋“ค ํŒŒ์ด์ฌ์œผ๋กœ ์ฝ”ํ…Œํ•ด์„œ C++ ์“ฐ์‹œ๋Š” ๋ถ„์ด ๋ณด์ด๋ฉด ๊ดœํžˆ ๋ฐ˜๊ฐ‘๋„ค์š” :)

int main()
{
ios_base::sync_with_stdio(false); cin.tie(NULL);
stack <double> s;

Choose a reason for hiding this comment

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

์‚ฌ์‹ค stack์„ ๊ตณ์ด includeํ•  ํ•„์š”๋Š” ์—†์Šต๋‹ˆ๋‹ค. vector๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์Šคํƒ๊ณผ ์™„๋ฒฝํ•˜๊ฒŒ ๋™์ผํ•œ ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๊ฑฐ๋“ ์š” :)
stack์ด๋‚˜ vector ๋‘˜ ๋‹ค back์—์„œ ์‚ฝ์ž…๋˜๋ฉฐ, back์—์„œ popํ•˜๋Š” ๊ฒŒ $O(1)$ ์‹œ๊ฐ„์œผ๋กœ ๋™์ผํ•˜์ฃ .
๊ทธ๋ฆฌ๊ณ  vector๋ฅผ ์Šคํƒ์ฒ˜๋Ÿผ ์“ฐ๋ฉด ์ข‹์€ ์ ์ด 1. ์ˆœํšŒ๊ฐ€ ๊ฐ€๋Šฅํ•˜๊ณ  2. ์ธ๋ฑ์Šค๋กœ ์ค‘๊ฐ„ ์š”์†Œ์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

vector<double> stack;

for (int i = 0; i < n; i++) //ํ”ผ์—ฐ์‚ฐ์ž(์•ŒํŒŒ๋ฒณ)์— ๋Œ€์‘๋˜๋Š” ๊ฐ’ ์ €์žฅํ•ด๋†“๊ธฐ
cin >> alphabet[i];

for (int i = 0; i < postfix.length(); i++)

Choose a reason for hiding this comment

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

ranged-loop๋กœ ๋ณ€๊ฒฝํ•˜๋ฉด ์ข€ ๋” ๊ฐ„ํŽธํ•˜๊ฒŒ ๊ฐ’์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™๋„ค์š” :)

for(const char& op : postfix) {
    ...
}

Comment on lines +22 to +25
if (postfix[i] >= 'A' && postfix[i] <= 'Z') //ํ”ผ์—ฐ์‚ฐ์ž๋ผ๋ฉด ํ•ด๋‹น ๊ฐ’ push
s.push(alphabet[postfix[i] - 'A']);

else //์—ฐ์‚ฐ์ž๋ฅผ ๋งŒ๋‚œ๋‹ค๋ฉด ์Šคํƒ์˜ top 2๊ฐœ๋ฅผ ๊บผ๋‚ด์–ด ์—ฐ์‚ฐ ์ˆ˜ํ–‰ํ›„ ํ•ด๋‹น ๊ฐ’ push

Choose a reason for hiding this comment

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

์—ฌ๊ธฐ else ๋ถ€๋ถ„์„ ์œ„ if๋ฌธ์—์„œ continue๋กœ ๋„˜๊ฒจ๋ฒ„๋ฆฌ๋Š” ๊ฒƒ๋„ ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”.

if('A' <= op && op <= 'Z') {
    s.push(alphabet[op - 'A']);
    continue;
}

// ๊ธฐ์กด else ๋ถ€๋ถ„..

Copy link
Member Author

Choose a reason for hiding this comment

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

์˜คํ˜ธ vector๋ฅผ stack์ฒ˜๋Ÿผ ์“ฐ๋Š”๊ฑฐ๋Š” ์ƒ๊ฐ๋„ ์•ˆ ํ•ด๋ดค๋Š”๋ฐ ๊นจ๋‹ซ๊ณ  ๋จธ๋ฆฌ๋ฅผ ํƒ์น˜๊ฒŒ ๋˜๋„ค์š”๐Ÿ˜ฎ
ranged-loop๋Š” ์–ด์ƒ‰ํ•ด์„œ ์ž˜ ์•ˆ ์“ฐ๊ฒŒ๋˜๋Š”๋ฐ ์ด์ œ ์ž์ฃผ ์‚ฌ์šฉํ•ด๋ด์•ผ๊ฒ ๋„ค์š”!!
ํ”ผ๋“œ๋ฐฑ์ด๋ž‘ ๊ฟ€ํŒ ์™„์ „ ๊ฐ์‚ผ๋  :)

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.

5 participants