본문 바로가기

백준33

백준 1083번 : 소트 (Python) 출처 : https://www.acmicpc.net/problem/1083 n = int(input()) nums = list(map(int, input().split())) s = int(input()) # 배열 완료된 숫자 개수 done = 0 # 이상적 배열 ideal = sorted(nums, reverse=True) while s > 0 : if nums == ideal : break range_max = max(nums[done : min(n, done+s+1)]) # print("배열 완료된 숫자 개수", done) # print("현 범위", nums[done : min(n, done+s+1)]) # print("현 범위 최대", range_max) idx = nums[done:].inde.. 2022. 12. 7.
백준 1181번 : 단어 정렬 (Python) 출처 : https://www.acmicpc.net/problem/1181 n = int(input()) words = set() for i in range(n) : words.add(input()) words = sorted(words) words = sorted(words, key = len) print(*words, sep="\n") 중복된 단어는 출력하지 않으므로 집합에 단어를 추가한다. 처음 정렬로 단어들이 알파벳 순으로 정렬되고, 두번째 정렬로 길이 순으로 정렬된다. 길이 순 정렬을 나중에 했기 때문에 길이가 같은 단어의 경우에는 알파벳 순서가 앞서는 단어가 먼저온다. 2022. 12. 5.
백준 11729번 : 하노이의 탑 이동 순서 (Python) 출처 : https://www.acmicpc.net/problem/11729 def hanoi(n, start, end) : if n == 1 : print(start, end) else : hanoi(n-1, start, 6 - start - end) print(start, end) hanoi(n-1, 6 - start - end, end) n = int(input()) print(2**n - 1) hanoi(n, 1, 3) n은 전체 원판의 수, start는 옮길 원판이 꽂혀있는 막대의 번호, end는 원판을 옮길 막대의 번호이다. 총 n개의 원판이 있을 때, 전체 과정은 크게 3단계로 구성된다. 1. n-1 개의 원판을 start도 end도 아닌 제 3의 막대(이 막대의 번호는 6 - start -.. 2022. 12. 4.
백준 10870번 : 피보나치 수 5 (Python) https://www.acmicpc.net/problem/10870 def fib(n) : if n == 0 or n == 1 : return n else : return fib(n - 1) + fib(n - 2) n = int(input()) print(fib(n)) 가장 기본적인 재귀 알고리즘 문제이다. 함수의 출력값으로 그 함수를 다시 주면 알아서 답이 나올때까지 계산한다. 참고로 마지막에 print를 안 붙이고 fib(n)만 입력하면 백준에선 틀렸다고 채점한다. 2022. 11. 20.