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

3-g0rnn #9

Merged
merged 2 commits into from
Oct 29, 2024
Merged

3-g0rnn #9

merged 2 commits into from
Oct 29, 2024

Conversation

g0rnn
Copy link
Collaborator

@g0rnn g0rnn commented Oct 4, 2024

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

[๋ฌธ์ž์—ด] ์žƒ์–ด๋ฒ„๋ฆฐ ๊ด„ํ˜ธ : https://www.acmicpc.net/problem/1541

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

1h +

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

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

์•„์ด๋””์–ด๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.
๋ฌธ์ž์—ด์ด 10+43-12-33+12+33+4-3+2 ์™€ ๊ฐ™์ด ์ฃผ์–ด์ง„๋‹ค๋ฉด 
10+43  -   12   -   (33+12+33+4)   -   3+2 ์™€ ๊ฐ™์ด -๋กœ ๋‚˜๋ˆ„์–ด์„œ ๋ง์…ˆ์„ ์ง„ํ–‰ํ•˜๊ณ 
๋‚˜๋จธ์ง€ ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ๊ฐ€์žฅ ํฐ ์ˆ˜๋“ค๋กœ ๋บ„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

1. ๋ฌธ์ž์—ด์„ -๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋‚˜๋ˆ„์–ด์ค๋‹ˆ๋‹ค.
2. ๋‚˜๋ˆ„์–ด์ง„ ๋ถ€๋ถ„์— ๋Œ€ํ•ด ์ˆœํšŒํ•˜๋ฉฐ ๋”ํ•ฉ๋‹ˆ๋‹ค.
3. ๋งจ ์ฒ˜์Œ ์ˆซ์ž๋Š” ๋ถ€ํ˜ธ๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์— ๋งจ ์ฒ˜์Œ ์ˆซ์ž์— ๋‚˜๋จธ์ง€ ์ˆ˜๋“ค์„ ๋บ๋‹ˆ๋‹ค.

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

Java์—๋Š” ๋ฌธ์ž์—ด์„ ๋‚˜๋ˆ„๋Š” ๋ฐฉ๋ฒ•์ด 2๊ฐ€์ง€์ด๋‹ค.

1. split

  • split์€ ์ •๊ทœ์‹(regex)๋ฅผ ๋ฐ›๊ธฐ ๋•Œ๋ฌธ์— +์™€ ๊ฐ™์€ ๋ฉ”ํƒ€๋ฌธ์ž(ํŠน๋ณ„ํ•œ ์˜๋ฏธ๋ฅผ ๊ฐ€์ง„ ๋ฌธ์ž)๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ์ด์Šค์ผ€์ดํ”„ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์•ผํ•œ๋‹ค. (\๋ฅผ ๋ถ™์ด๋ฉด ๋จ)
  • ""๋ฅผ ๊ตฌ๋ถ„์ž๋กœ ์ธ์‹ํ•œ๋‹ค.
  • ๋ฌธ์ž์—ด ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

2. StringTokenizer

  • hasMoreTokens()๋‚˜ nextToken()๊ณผ ๊ฐ™์ด ๋‹ค์–‘ํ•œ ๋ฉ”์„œ๋“œ ๋“ค์ด ์žˆ๋‹ค.
  • ""๋ฅผ ๊ตฌ๋ถ„์ž๋กœ ์ธ์‹ํ•˜์ง€ ์•Š๋Š”๋‹ค.
  • ๋ฐ˜ํ™˜๊ฐ’์ด String์ด๋‹ค. ๊ทธ๋ž˜์„œ ๋ฐ˜๋ณต๋ฌธ์„ ํ™œ์šฉํ•˜์—ฌ ์—ฌ๋Ÿฌ๋ฒˆ ์ถ”์ถœํ•ด์•ผํ•œ๋‹ค.

Copy link
Collaborator

@kangrae-jo kangrae-jo left a comment

Choose a reason for hiding this comment

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

์ €๋„ ์ฒ˜์Œ ๋ณด๊ณ ๋Š” ํ•˜๋‚˜ํ•˜๋‚˜ ํŒŒ์‹ฑํ•ด์„œ ๊ฒฝ์šฐ์˜์ˆ˜๋ฅผ ๋”ฐ์ง€๋Š” ๋ฌธ์ œ์ธ์ง€ ์•Œ์•˜์Šต๋‹ˆ๋‹ค.
๊ทผ๋ฐ ๊ท ํ˜ธ๋‹˜ ์„ค๋ช…์„ ๋ณด๊ณ , ์กฐ๊ธˆ๋” ์ƒ๊ฐํ•ด๋ณด๋‹ˆ๊นŒ
๊ธฐํ˜ธ๊ฐ€ + - ๋ฐ–์— ์—†๋Š” ์กฐ๊ฑด์— ๊ด„ํ˜ธ๋ฅผ ๋งˆ์Œ๋Œ€๋กœ ์น  ์ˆ˜ ์žˆ๋‹ค๋ฉด - ๊ธฐ์ค€์œผ๋กœ ๋ฌถ์Œ ๋ฌถ์Œ ์ฒ˜๋ฆฌํ•ด์„œ ์—ฐ์‚ฐํ•œ๋‹ค๋ฉด, ์ตœ์†Œ ํ•ฉ์ด ๊ตฌํ•ด์ง„๋‹ค๋Š”๊ฒŒ ์ดํ•ด ๋์Šต๋‹ˆ๋‹ค.

int minSum = 0;
string num;
bool minusFlag = false;

    for (int i = 0; i <= str.size(); i++) {
        if (str[i] == '+' || str[i] == '-' || i == str.size()) {
            if (minusFlag) minSum -= stoi(num);
            else minSum += stoi(num);
            num = "";
        } 
        else num += str[i];

        if (str[i] == '-') minusFlag = true;
    }
    cout << minSum;

์ž…๋ ฅ๋ฐ›์€ ๋ฌธ์ž์—ด์„ ์ˆœํšŒํ•˜๋ฉฐ
๊ธฐํ˜ธ๊ฐ€ ๋‚˜์˜ฌ๋•Œ ๊นŒ์ง€๋Š” ์ˆ˜๋ฅผ ์ •์ˆ˜ํ™” ํ•ด๋†“์•˜๋‹ค๊ฐ€
๊ธฐํ˜ธ๊ฐ€ ๋‚˜์˜ค๋ฉด minusFlag์— ๋”ฐ๋ผ + -ํ•˜๋Š” ๋ฐฉ์‹์˜ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค.

ํ•ต์‹ฌ์€ - ๊ฐ€ ํ•œ๋ฒˆ ๋‚˜์˜ค๋ฉด ๋’ค์—๋Š” ๋ชจ๋‘ ๊ด„ํ˜ธ๋ฅผ ํ™œ์šฉํ•ด์„œ - ์ฒ˜๋Ÿผ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด๋ผ๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค.

Copy link
Collaborator

@kangrae-jo kangrae-jo Oct 5, 2024

Choose a reason for hiding this comment

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

์„ค๋ช…ํ•˜์‹ ๋Œ€๋กœ - ๊ธฐ์ค€์œผ๋กœ ๋ฌธ์ž์—ด์„ ์ž๋ฅด๊ณ 
์ž˜๋ผ์ง„ ๋ฉ์–ด๋ฆฌ๋ผ์ง€ ํ•ฉํ•ด์„œ - ๋ฅผ ํฌ๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ์‹์ด๊ตฐ์š” ์ž˜๋ดค์Šต๋‹ˆ๋‹ค.

์ž๋ฐ”์ฝ”๋“œ๋กœ ๋ณด๋‹ˆ ํ•œ๋ˆˆ์— ์•ˆ๋“ค์–ด์˜ค๊ธด ํ•˜๋Š”๋ฐ ๋•๋ถ„์— ์ž๋ฐ” ๊ณต๋ถ€ํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

๋ฌธ์ž์—ด์„ tokenizeํ•˜๋Š”๊ฒŒ ์•„๋‹ˆ๋ผ ํ•œ๋ฒˆ์— ๋Œ๋ฉด์„œ ๋ฌธ์ œ๋ฅผ ํ‘ธ์…จ๊ตฐ์š”! ์ด ๋ฐฉ์‹์ด ์ฝ”๋“œ ์†๋„๋Š” ์ œ์ผ ๋น ๋ฅผ ๊ฑฐ ๊ฐ™๋„ค์š” ใ„ทใ„ท

@kokeunho
Copy link
Collaborator

ํ‰์†Œ์—๋„ ์ž์ฃผ ์“ฐ๋˜ split์„ ์‚ฌ์šฉํ•˜๋ฉด ์‰ฝ๊ฒŒ ํ’€ ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ๋ผ๊ณ  ์ƒ๊ฐ์ด ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค.

expression = input()

part = expression.split('-')

result = sum(map(int, part[0].split('+')))

for i in range(1, len(part)):
    result -= sum(map(int, part[i].split('+')))

print(f'{result}')

sum, map, split ๋งŽ์ด ์“ฐ๋Š”๋ฐ
๋” ํ™œ์šฉ ์ž˜ ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋œ ๋ฌธ์ œ์˜€์Šต๋‹ˆ๋‹ค.
์ˆ˜๊ณ ํ•˜์…จ์Šต๋‹ˆ๋‹ค!

Copy link
Collaborator

@wnsmir wnsmir left a comment

Choose a reason for hiding this comment

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

์ฒ˜์Œ์— -๊ฐ€ ๋‚˜์˜ค๋ฉด ๊ด„ํ˜ธ๋ฅผ ์—ด๊ณ  ๋‹ค์Œ ๋งˆ์ด๋„ˆ์Šค๊ฐ€ ๋‚˜์˜ค๋ฉด ๊ด„ํ˜ธ๋ฅผ ๋‹ซ๊ณ  ๋‹ค์‹œ ์—ฌ๋Š”๊ฑธ ๋ฐ˜๋ณตํ•˜๋‹ค๊ฐ€ ๋งˆ์ง€๋ง‰๊นŒ์ง€ ๊ด„ํ˜ธ๊ฐ€ ๋‹ซํžˆ์ง€ ์•Š์œผ๋ฉด ๊ด„ํ˜ธ๋ฅผ ๋‹ซ์•„์ฃผ๋Š” ๋ฐฉ์‹์œผ๋กœ ๋กœ์ง์„ ๊ตฌํ˜„ํ•˜๋ ค ํ–ˆ๋Š”๋ฐ ๋ฐ˜๋ก€๊ฐ€ ๋„ˆ๋ฌด ๋งŽ์ด ๋‚˜์™€ ์ด๋ฐฉ์‹์„ ํฌ๊ธฐํ–ˆ์Šต๋‹ˆ๋‹ค.

์ดํ›„ ์ƒ๊ฐํ•ด๋ณด๋‹ค ์šฐํ…Œ์ฝ” ํ”„๋ฆฌ์ฝ”์Šค 1์ฃผ์ฐจ๋•Œ ์‚ฌ์šฉํ–ˆ๋˜ split์„ ์‚ฌ์šฉํ•ด์•ผ๊ฒ ๋‹ค ์‹ถ์–ด์„œ -๊ธฐ์ค€์œผ๋กœ ์Šคํ”Œ๋ฆฟ์œผ๋กœ ๋‚˜๋ˆˆ๋’ค ๊ฐ ํŒŒํŠธ๋ณ„๋กœ ๋”ํ•ด์ฃผ๊ณ  ๋งˆ์ด๋„ˆ์ŠคํŒŒํŠธ ์ฆ‰ ์ง์ˆ˜๋ฒˆ ํŒŒํŠธ๋Š” ๋นผ์ฃผ๋Š” ๋ฐฉ์‹์œผ๋กœ ์ฝ”๋“œ๋ฅผ ๊ตฌํ˜„ํ–ˆ์Šต๋‹ˆ๋‹ค.

ํ›จ์”ฌ ๊ฐ„๋‹จํ•ด์ง€๊ณ  ์˜ˆ์™ธ๋„ ์—†์Šต๋‹ˆ๋‹ค. ์Šคํ”Œ๋ฆฟ์˜ ๊ฐ•์ ์„ ๋Š๋ผ๊ฒŒ ํ•ด์ฃผ๋Š” ๋ฌธ์ œ์˜€์Šต๋‹ˆ๋‹ค.

# ์ž…๋ ฅ๋ฐ›๊ธฐ
expression = input()

# '-'๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ˆ˜์‹์„ ๋ถ„๋ฆฌ
parts = expression.split('-')

# ์ฒซ ๋ฒˆ์งธ ๋ถ€๋ถ„์€ ์–‘์ˆ˜๋กœ ๊ณ„์‚ฐ
result = sum(map(int, parts[0].split('+')))

# ๋‘ ๋ฒˆ์งธ ๋ถ€๋ถ„๋ถ€ํ„ฐ๋Š” ๋ชจ๋‘ ์Œ์ˆ˜๋กœ ๊ณ„์‚ฐํ•˜์—ฌ ๋นผ์คŒ
for part in parts[1:]:
    result -= sum(map(int, part.split('+')))

# ๊ฒฐ๊ณผ ์ถœ๋ ฅ
print(result)

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