본문 바로가기

Algorithm

(42)
[정렬] 선택정렬 (Select Sort) 선택정렬 O(n^2) 오름차순을 기준으로 할 때 최소값 및 우선순위가 높은 값을 인덱스 0 방향으로 가깝게 배치 기준이 되는 인덱스와 최소값이 되는 인덱스를 swap 첫번째 하나 뽑고, 최소값을 구하고 비교하여 swap 첫번째 for 0
[정렬] 버블정렬 (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
[백준] 10773 제로 Python C++ 1 2 3 4 5 6 7 8 9 n = int(input()) result = [] for i in range(n): inputs = int(input()) if inputs == 0: result.pop() else: result.append(inputs) print(sum(result)) 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 33 34 35 36 37 38 39 40 41 #include #include #include #include #include #include #include #include #include #include using namespace std; int main(..
[백준] 11047 동전0 Python C++ 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 n, k = map(int, input().strip().split()) inputs = [] cnt = 0 point = 0 temp = 0 for i in range(n): temp = int(input()) inputs.append(temp) if temp > k; int *inputs = new int[n]; for (int i = 0; i > *(inputs + i); if (k >= *(inputs + i)) point = i; //cout
[백준] 2581 소수 Python (주어진 범위 내 소수들의 합, 최소값) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 m = int(input()) n = int(input()) lists = [] for i in range(m, n+1): check = 0 if i >= 2: if i == 2: lists.append(i) else: for j in range(2, i): if i%j == 0: check = 1 break if check == 0: lists.append(i) if len(lists) >= 1: print(sum(lists)) print(min(lists)) else: print(-1)
[백준] 소수찾기 1978 Python C++ Python 1234567891011121314151617tc = int(input())inputs = list(map(int, input().strip().split()))cnt = 0 for i in inputs: nop = 0 if i>=2: if i == 2: cnt +=1 else: for j in range(2,i): if i%j == 0: nop = 1 break if nop == 0: cnt +=1print(cnt)Colored by Color Scriptercs C++ 1234567891011121314151617181920212223242526272829303132333435#include using namespace std; int main(){ int n; int tc; cin >>..
[백준] 2921 도미노 Python 123456789101112n = int(input())a = [0,0]summ = 0 while a[0] != n: if a[0] == a[1]: a[1] += 1 a[0] = 0 summ += (a[0]+a[1]) a[0] +=1 summ += (a[0] + a[1])print(summ)cs
[백준] 주사위 세개 2480 Python C++ Python 1234567891011121314a = list(map(int, input().strip().split())) if a[0] == a[1] == a[2]: print(10000+(a[0]*1000))elif a.count(a[0]) == 2 or a.count(a[1]) == 2 or a.count(a[2]) == 2: if a.count(a[0]) == 2: print(1000 + (a[0] * 100)) elif a.count(a[1]) == 2: print(1000 + (a[1] * 100)) elif a.count(a[2]) == 2: print(1000 + (a[2] * 100))else: print(max(a)*100) Colored by Color Scriptercs C++ 1..