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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion oesnuj/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

| ์ฐจ์‹œ | ๋‚ ์งœ | ๋ฌธ์ œ์œ ํ˜• | ๋งํฌ | ํ’€์ด |
|:----:|:---------:|:----:|:-----:|:----:|
| 1์ฐจ์‹œ | 2023.10.27 | BFS | - | - |
| 1์ฐจ์‹œ | 2024.03.26 | ์Šคํƒ | [ํ›„์œ„ํ‘œ๊ธฐ์‹2]https://www.acmicpc.net/problem/1935 | [#4]https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/4 |
---
43 changes: 43 additions & 0 deletions oesnuj/์Šคํƒ/1935.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#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;

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;


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++)

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) {
    ...
}

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

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

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๋Š” ์–ด์ƒ‰ํ•ด์„œ ์ž˜ ์•ˆ ์“ฐ๊ฒŒ๋˜๋Š”๋ฐ ์ด์ œ ์ž์ฃผ ์‚ฌ์šฉํ•ด๋ด์•ผ๊ฒ ๋„ค์š”!!
ํ”ผ๋“œ๋ฐฑ์ด๋ž‘ ๊ฟ€ํŒ ์™„์ „ ๊ฐ์‚ผ๋  :)

{
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(); //์†Œ์ˆ˜์  ๋‘˜์งธ์งœ๋ฆฌ ๊นŒ์ง€ ์ถœ๋ ฅ
return 0;
}