728x90 반응형 2025/025 4. 후위연산(stack)_파이썬 🚀전체 코드import syssys.stdin=open("C:\\input.txt",'rt')a = input()print(a)stack = []res = ''for x in a: # x가 숫자일 경우 if x.isdecimal(): stack.append(int(x)) # x가 숫자가 아닐 경우 else: if x =='+': n1 = stack.pop() n2 = stack.pop() stack.append(n2+n1) elif x =='-': n1 = stack.pop() n2 = stack.pop() stack.appen.. 2025. 2. 13. 3. 후위 표기식 만들기 : infix-->postfix (스택) ✅ 전체코드import sysfrom unicodedata import decimalsys.stdin=open("C:\\\\input.txt",'rt')a = input()stack = []res=''print(a)for x in a: # 입력값 x 가 숫자라면 if x.isdecimal(): res += x # x가 숫자가 아니라면면 else: if x == '(': stack.append(x) elif x=='*' or x =='/': while stack and (stack[-1] == '*' or stack[-1] =='/'): res += stack.pop() .. 2025. 2. 13. 2. 쇠막대기(스택)_python 전체코드import syssys.stdin=open("C:\\input.txt",'rt')# input : ()(((()())(())()))(()) # 결과 : 17s = input()stack = []cnt =0for i in range(len(s)): #열린 괄호 if s[i] =='(': stack.append(s[i]) #닫는 괄호 else: stack.pop() # 바로 앞이 열린 괄호일때 if s[i-1] == '(': cnt += len(stack) # 바로 앞이 닫힌 괄호일때 else: cnt+=1print(cnt) 괄호 문자열 ()(((()())(()).. 2025. 2. 13. 1. 가장 큰 수(스택)_python 전체코드 풀이가장큰수.pyimport syssys.stdin=open("C:\\input.txt",'rt')num, m =map(int,input().split())print("num , m :", num,m)num = list(map(int, str(num)))print("num : " , num)stack = []for x in num: print(x) while stack and m > 0 and x > stack[-1]: stack.pop() m-=1 stack.append(x)if m != 0: stack = stack[:-2]# 출력방법1: res = ''.join(map(str,stack))# print(res)# 출력방법2for x in st.. 2025. 2. 13. 이전 1 2 다음 728x90 반응형