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-xxubin04 #3

Merged
merged 2 commits into from
Nov 9, 2023
Merged

1-xxubin04 #3

merged 2 commits into from
Nov 9, 2023

Conversation

xxubin04
Copy link
Member

@xxubin04 xxubin04 commented Nov 8, 2023

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

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

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

5๋ถ„

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

  • ๋ฌธ์ œ๋ฅผ ์ฝ๊ณ  ์ดํ•ดํ•˜๋ฉด ์‰ฝ๊ฒŒ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ์ด๋‹ค. 2๋ฒˆ์—์„œ ํ—ท๊ฐˆ๋ฆด ์ˆ˜ ์žˆ์œผ๋‹ˆ ์˜ˆ์ œ๋ฅผ ์ž˜ ๋ณด์ž.
  • ๋จผ์ €, ๋ฐ˜๋ณตํ•  ํšŸ์ˆ˜๋ฅผ ์ž…๋ ฅ๋ฐ›๊ณ  for๋ฌธ์„ ๋งŒ๋“ ๋‹ค.
    import sys

    n = int(sys.stdin.readline())
    stack = []

    for i in range(n):
        num = sys.stdin.readline().split()
  • ๋ช…๋ น 5๊ฐ€์ง€๋ฅผ ์ถฉ์กฑํ•˜๋Š” if๋ฌธ์„ ์ž‘์„ฑํ•œ๋‹ค.
    if num[0] == "1":
        stack.append(num[-1])

    elif num[0] == "2":
        if len(stack) != 0:
            print(stack.pop(-1))
        else:
            print(-1)

    elif num[0] == "3":
        print(len(stack))

    elif num[0] == "4":
        if len(stack) == 0:
            print(1)
        else:
            print(0)

    elif num[0] == "5":
        if len(stack) != 0:
            print(stack[-1])
        else:
            print(-1)
  • 1๋ถ€ํ„ฐ 5๋ฅผ ์ œ์™ธํ•œ ์ˆซ์ž๊ฐ€ num์— ์ž…๋ ฅ๋  ์ˆ˜ ์žˆ์œผ๋‹ˆ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ง€์ •ํ•œ๋‹ค.
    else:
        break
  • ์ „์ฒด ์ฝ”๋“œ๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค.
  import sys
  
  n = int(sys.stdin.readline())
  stack = []
  
  for i in range(n):
      num = sys.stdin.readline().split()
  
      if num[0] == "1":
          stack.append(num[-1])
  
      elif num[0] == "2":
          if len(stack) != 0:
              print(stack.pop(-1))
          else:
              print(-1)
  
      elif num[0] == "3":
          print(len(stack))
  
      elif num[0] == "4":
          if len(stack) == 0:
              print(1)
          else:
              print(0)
  
      elif num[0] == "5":
          if len(stack) != 0:
              print(stack[-1])
          else:
              print(-1)
      else:
          break

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

  • ๊ณ„์† ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋˜์–ด์„œ ์™œ์ธ์ง€ ์ฐพ์•„๋ณด์•˜๋‹ค.
    input()์„ ์ด์šฉํ•ด์„œ ๋ฐ˜๋ณต๋ฌธ์œผ๋กœ ์—ฌ๋Ÿฌ ์ค„์„ ์ž…๋ ฅ๋ฐ›์œผ๋ฉด ๊ทธ๋Ÿฌํ•˜๋‹ค๊ณ  ํ•œ๋‹ค.
    ๋”ฐ๋ผ์„œ, ๋ฐ˜๋ณต๋ฌธ + ์—ฌ๋Ÿฌ ์ค„ ์ž…๋ ฅ -> sys.stdin.readline()
    (https://velog.io/@yeseolee/Python-%ED%8C%8C%EC%9D%B4%EC%8D%AC-%EC%9E%85%EB%A0%A5-%EC%A0%95%EB%A6%ACsys.stdin.readline)

@xxubin04 xxubin04 changed the title 2023-11-08 ์Šคํƒ 2 1-xxubin04 Nov 8, 2023
Copy link
Collaborator

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

๊ณ ์ƒ ๋งŽ์•˜์Šต๋‹ˆ๋‹ค!
์ž๋ฃŒ๊ตฌ์กฐ ์ˆ˜์—…์„ ์•„์ง ์ˆ˜๊ฐ•ํ•˜์ง€ ์•Š์•„์„œ ์Šคํƒ๊ฐ™์€ ํŠน์ˆ˜ํ•œ ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฌธ์ œ๋ฅผ ํ’€๊ธฐ ์‰ฝ์ง€ ์•Š์•˜์„ ํ…๋ฐ, ์ด๋ฏธ 1ํ•™๋…„์—์„œ ์ƒ์œ„ 10%์— ์†ํ•˜๋„ค์š” ๐Ÿ˜Ž

stack = []

for i in range(n):
num = sys.stdin.readline().split()
Copy link
Collaborator

Choose a reason for hiding this comment

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

  • sys.stdin ์ด ์นœ๊ตฌ๋Š” python์—์„œ "๋น ๋ฅธ ์ž…๋ ฅ"์„ ๋ฐ›๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ์— readline()์„ ์‚ฌ์šฉํ•ด์„œ ํ•œ ๋ฒˆ์— ์ž…๋ ฅ์„ ๋ฐ›์„ ํ•„์š”๊นŒ์ง€๋Š” ์—†์–ด๋ณด์ž…๋‹ˆ๋‹ค.
  • ์ œ๊ฐ€ ํ™•์ธํ•ด๋ณธ ๊ฒฐ๊ณผ, ๋น ๋ฅธ ์ž…๋ ฅ์„ ์‚ฌ์šฉํ•ด์ฃผ๊ธฐ๋งŒ ํ•˜๋ฉด ์ˆซ์ž๋ฅผ ํ•˜๋‚˜์”ฉ ๋”ฐ๋กœ ์ž…๋ ฅ๋ฐ›๋”๋ผ๋„ ๋ฌด๋‚œํžˆ ํ†ต๊ณผํ•˜๋„ค์š”.
ios_base::sync_with_stdio(false);  //  cpp์—์„œ ๋น ๋ฅธ ์ž…๋ ฅ์„ ๋ฐ›๊ธฐ ์œ„ํ•œ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค.
cin.tie(nullptr);
cout.tie(nullptr);

int N; cin >> N;
vector<int> stack;
for(int i = 0; i < N; i++) {
  int n; cin >> n;
  ... // ๋กœ์ง ์ˆ˜ํ–‰
}

Copy link
Member Author

Choose a reason for hiding this comment

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

์˜คํ˜ธ ๊ทธ๋ ‡๊ตฐ์š”! ์ข‹์€ ๋ง์”€ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค~๐Ÿ˜Š๐Ÿ‘

Copy link
Member

Choose a reason for hiding this comment

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

์•„๋‹ˆ์š”. ํŒŒ์ด์ฌ์—์„œ๋Š” ํ•˜๋‚˜์”ฉ ์ž…๋ ฅ๋ฐ›๋Š” ๊ธฐ๋Šฅ์ด ์—†์Šต๋‹ˆ๋‹ค. ๐Ÿ˜ฅ

Copy link
Collaborator

@Dolchae Dolchae 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 +13 to +16
if len(stack) != 0:
print(stack.pop(-1))
else:
print(-1)
Copy link
Member

Choose a reason for hiding this comment

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

if len(stack) != 0 ์„ if stack์œผ๋กœ ์ค„์ผ ์ˆ˜ ์žˆ์–ด์š”.

Copy link
Member Author

Choose a reason for hiding this comment

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

์•— ๊ทธ๋ ‡๊ตฐ์š”!! ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค๐Ÿ˜Š
๋‹ค์Œ๋ถ€ํ„ฐ๋Š” ๊ทธ๋ ‡๊ฒŒ ์“ฐ๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค!

@xxubin04 xxubin04 merged commit 3418d62 into AlgoLeadMe:main Nov 9, 2023
@9kyo-hwang 9kyo-hwang mentioned this pull request Feb 11, 2024
@Dolchae Dolchae mentioned this pull request Feb 19, 2024
@Dolchae Dolchae mentioned this pull request Mar 4, 2024
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