[백준] 하노이 탑 이동 순서 11729 Python (재귀)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 cnt = 0 result = [] def hanoi(n, fromm, by, to): global cnt cnt += 1 if n == 1: result.append(str(fromm)+" "+str(to)) else: hanoi(n-1, fromm, to, by) result.append(str(fromm)+" "+str(to)) hanoi(n-1, by, fromm, to) n = int(input()) hanoi(n,1,2,3) print(cnt) for i in result: print(i)
[정렬] 버블정렬 (Bubble Sort)
버블정렬 O(n^2) 마지막 인덱스에 최대 값을 놓음 첫번째 for 조건 0 3,4,2,1 -> 3,2,4,1 -> 3,2,1,4 두번째 맨 마지막 숫자를 제외하고 3-2, 2-1와 같이 비교 해야하는 갯수가 정해짐 3,2,1,4 -> 2,3,1,4 -> 2,1,3,4 세번째 맨 마지막 숫자를 제외하고 2-1와 같이 비교 해야하는 갯수가 정해짐 2,1,3,4 -> 1,2,3,4 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 #include using namespace std; void BubbleSort(int arr[], int n){ int temp; for (int i = 0; i