반응형

python 12

[알고리즘 유형 / Python] 그리디 이론

그리디(Greedy) 알고리즘은 지금 이 순간, 지금 당장, 최적의 답을 찾는 방법이라고 한다. 지금 당장에서는 최적의 방법이지만 나중과 종합적으로 봤을 때는 최적이 아닐 수 있다. 욕심쟁이 알고리즘, 탐욕법 등의 이름으로 불린다. 그리디 알고리즘으로 풀어야하는 대표적인 문제 유형으로는 거스름돈 활동 선택 문제 최소 신장 트리 등등의 유형들이 있다. 거스름돈 당신은 음식점의 계산을 도와주는 점원이다. 카운터에는 거스름돈으로 사용할 500원, 100원, 50원, 10원짜리 동전이 무한히 존재한다고 가정한다. 손님에게 거슬러 줘야 할 돈이 N원일 때 거슬러 줘야 할 동전의 최소 개수를 구하라. 단, 거슬러 줘야 할 돈 N은 항상 10의 배수이다. 거스름돈 유형에서는 가장 큰 화폐 단위부터 돈을 거슬러 주는 ..

Algorithm/그리디 2022.01.08

[백준 알고리즘 / Python] 기본 수학_1 - 1712번 손익분기점

고정비용 A, 가변비용 B, 판매가격 C 로 변수에 입력받았다. A, B, C = map(int, input().split()) if A/(C-B) < 0: print(-1) else: count = 1 while True: if A+(B*count) < C*count: break count += 1 print(count) 처음에는 단순하게 while문을 돌려서 손익분기점(count)을 찾아갔는데 시간초과가 났다. 나와 비슷한 사람이 질문한 글을 보고 수학적으로 생각해야된다는 답변이 있어 다시 풀었다. A, B, C = map(int, input().split()) if C-B < 0: print(-1) else: print((A // (C-B)) + 1) 분명 맞게 했는데 이번엔 런타임 에러(ZeroD..

[백준 알고리즘/Python] 문자열

1단계 11654번 아스키 코드 character = input() print(ord(character)) ord 함수에 문자를 넣으면 해당 문자의 아스키 코드값을 반환한다. 2단계 10809번 숫자의 합 N = int(input()) num = input() sum = 0 for i in num: sum += int(i) print(sum) 3단계 10809번 알파벳 찾기 S = input() resultList = [ -1 for _ in range(26)] count = 0 for i in S: alphabet = ord(i) - 97 if resultList[alphabet] != -1: count += 1 continue resultList[alphabet] = count count += 1 f..

[백준 알고리즘/Python]1차원 배열

1단계 10818번 최소, 최대 n = int(input()) listA = list(map(int, input().split())) print(min(listA), max(listA)) 2단계 2562번 최댓값 listA = list() for i in range(9): listA.append(int(input())) print(max(listA)) print(listA.index(max(listA))+1) listA = [int(input()) for _ in range(9)] print(max(listA)) print(listA.index(max(listA))+1) 두 가지 방법으로 풀어보았다. 첫 번재 방법은 for문을 이용하여 listA를 만들었고, 두 번째 방법은 리스트 컴프리헨션(Compre..

[백준 알고리즘/Python] while문

1단계 10952번 A+B -5 import sys while 1: A, B = map(int, sys.stdin.readline().split()) if A == 0 and B == 0: break print(A+B) 2단계 10951번 A+B -4 import sys while 1: try: A, B = map(int, sys.stdin.readline().split()) print(A+B) except: break A와 B에 아무것도 입력되지 않는 경우 에러가 발생하므로 try~except로 예외 처리해서 아무 입력이 없을 경우 break로 while문을 탈출하고 종료된다. 3단계 1110번 더하기 사이클 N = int(input()) oldNum = N cycle = 0 while True: if..

[백준 알고리즘/Python] for문

1단계 2739번 구구단 N = int(input()) for i in range(1,10): print(N,'*',i,'=',N*i) N = int(input()) for i in range(1,10): print("%d * %d = %d" %(N,i,N*i)) print()를 두 가지 방법으로 출력해봤습니당 2단계 10950번 A+B -3 T = int(input()) for i in range(T): A,B = input().split() A = int(A) B = int(B) print(A+B) 3단계 8393번 합 n = int(input()) result = 0 for i in range(1, n+1): result += i print(result) 4단계 15552번 빠른 A+B import..

[백준 알고리즘/Python] 입출력과 사칙연산

1단계 2557번 Hello World print("Hello World!") 2단계 10718번 We love kriii print('강한친구 대한육군') print('강한친구 대한육군') 3단계 10171번 고양이 print("\\ /\\") print(" ) ( ')") print("( / )") print(" \\(__)|") 역슬래시(\)를 출력할 땐 \\ 두 개를 써줘야 \ 하나를 출력한다. 4단계 10172번 개 print("|\\_/|") print("|q p| /}") print("( 0 )\"\"\"\\") print("|\"^\"` |") print("||_/=\\\\__|") 큰따옴표(")를 출력할 땐 큰따옴표 앞에 역슬래시를 붙여서 출력한다. \" -> " 출력 5단계 1000번 ..

반응형