[백준] 17299, 1935, 1918, 10808, 10809, 10820, 2743, 11655, 10824, 11656, 2609, 6588 (파이썬)

Colacan·2022년 1월 30일
1

[백준]

목록 보기
14/43

오늘은 상당히 많이 시간을 투자했다. 연휴라서 혼자 되새겨볼 시간이 많아서 더 좋은 것 같다. 예전에 어렵게 생각했던 코드를 응용해서 새로운 답을 만들어가는 자신을 보면 조금은 실력이 늘어난 것 같다는 생각이 든다. 그래도 아직 막히는 경우가 많으니 좀 더 열심히 해나가야겠다.

백준 17299번 오등큰수

from sys import stdin
N = int(stdin.readline())
num = [i for i in map(int,stdin.readline().split())]
num_fre = dict()
for j in num:
    num_fre[j] = num_fre.get(j,0) + 1
stack = list()
answer =[-1] * N
for i in range(N):
    while stack and num_fre[num[stack[-1]]]<num_fre[num[i]]:
        answer[stack[-1]] = num[i]
        stack.pop()
    stack.append(i)
print(*answer)

백준 1935번 후위 표기식2

from sys import stdin
N = int(stdin.readline())
sic = stdin.readline().rstrip()
num_list = list()
stack = list()
for i in range(N):
    num_list.append(int(stdin.readline()))
for j in sic:
    if j.isupper():
        stack.append(num_list[ord(j)-65])
    else:
        str2 = stack.pop()
        str1 = stack.pop()
        if j == '+':
            stack.append(str1+str2)
        elif j == '-':
            stack.append(str1-str2)
        elif j == '*':
            stack.append(str1*str2)
        elif j == '/':
            stack.append(str1/str2)
print('%.2f' %stack[0])

백준 1918번 후위 표기식

from sys import stdin
sic = stdin.readline().rstrip()
stack = list()
ans = ''
for j in sic:
    if j.isupper():
        ans+=j
    else:
        if j == '(':
            stack.append(j)
        elif j=='*' or j=='/': # 우선순위가 낮은 +,- 출력
            while stack and (stack[-1]=='*' or stack[-1]=='/'):
                ans += stack.pop()
            stack.append(j)
        elif j=='+' or j=='-': # ( 나올때까지 출력
            while stack and stack[-1] != '(':
                ans += stack.pop()
            stack.append(j)
        elif j == ')': # ( 나올때까지 출력
            while stack and stack[-1] !='(':
                ans += stack.pop()
            stack.pop()
while stack:
    ans += stack.pop()
print(ans)

백준 10808번 알파벳 개수

from sys import stdin
S = stdin.readline().rstrip()
num = {'a': 0,'b': 0,'c': 0,'d': 0,'e': 0,'f': 0,'g': 0,'h': 0,'i': 0,'j': 0,'k': 0,'l': 0,'m': 0,'n': 0,'o': 0,'p': 0,'q': 0,'r': 0,'s': 0,'t': 0,'u': 0,'v': 0,'w': 0,'x': 0,'y': 0,'z': 0}
stack = list()
for i in S:
    num[i] = num.get(i,0)+1
for j in range(26):
    stack.append(num[chr(j+97)])
print(*stack)

백준 10809번 알파벳 찾기

from sys import stdin
S = stdin.readline().rstrip()
num = {'a': -1,'b': -1,'c': -1,'d': -1,'e': -1,'f': -1,'g': -1,'h': -1,'i': -1,'j': -1,'k': -1,'l': -1,'m': -1,'n': -1,'o': -1,'p': -1,'q': -1,'r': -1,'s': -1,'t': -1,'u': -1,'v': -1,'w': -1,'x': -1,'y': -1,'z': -1}
stack = list()
stack_num = list()
count = 0
for i in S:
    if i not in stack:
        num[i] = count
    stack.append(i)
    count += 1
for j in range(26):
    stack_num.append(num[chr(j+97)])
print(*stack_num)

백준 10820번 문자열 분석

from sys import stdin
while True:
    S = stdin.readline().rstrip('\n') # 공백을 다 없애버려서 초반에 오류
    up_count, down_count,num_count,blank_count = 0,0,0,0
    if not S:
        break
    for i in S:
        if i.isupper():
            up_count +=1
        elif i.islower():
            down_count +=1
        elif i.isdigit():
            num_count +=1
        elif i.isspace():
            blank_count+=1
    print(down_count, up_count, num_count, blank_count)

백준 2743번 단어 길이 재기

from sys import stdin
S = stdin.readline().rstrip()
print(len(S))

백준 11655번 ROT13

from sys import stdin
S = stdin.readline().rstrip()
ans = ''
for i in S:
    if i.isupper() and i <= 'M':
        ROT = chr(ord(i)+13)
    elif i.isupper() and i > 'M':
        ROT = chr(ord(i)-13)
    elif i.islower() and i <= 'm':
        ROT = chr(ord(i)+13)
    elif i.islower() and i > 'm':
        ROT = chr(ord(i)-13)
    elif i.isspace:
        ROT = i
    ans += ROT
print(ans)

백준 10824번 네 수

from sys import stdin
A,B,C,D = stdin.readline().split()
print(int(A+B)+int(C+D))

백준 11656번 접미사 배열

from sys import stdin
S = stdin.readline().rstrip()
stack = list()
for i in range(len(S)):
    stack.append(S[i:])
stack.sort()
for i in stack:
    print(i)

백준 2609번 최대공약수와 최소공배수

import math
num1,num2 = map(int,input().split())
ans1 = math.gcd(num1,num2)
ans2 = int(num1*num2/ans1)
print(ans1)
print(ans2)

백준 6588번 골드바흐의 추측

from sys import stdin
def prime(num):
    if num==1:
        return False
    else:
        for i in range(2, int(num**0.5)+1):
            if num%i==0:
                return False
        return True

while True:
    N = int(stdin.readline())
    if N==0:
        break
    a = N
    b = 0
    while True:
        if prime(a)==True and prime(b)==True:
            print(N,'=',int(b),'+',int(a))
            break
        else:
            a -= 1
            b += 1
profile
For DE, DA / There is no royal road to learning

0개의 댓글