본문 바로가기

Algorithm/BOJ 기초

(19)
[백준] 카드 역배치 10804 C++ https://www.acmicpc.net/problem/10804 12345678910111213141516171819202122232425262728293031323334353637#include #include using namespace std; int main(){ ios::sync_with_stdio(false); cin.tie(NULL); vector v; int array1[21] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20}; int A, B; int temp; for (int cnt = 0; cnt > A >> B; for (int i = A; i
[백준] 나이순 정렬 10814 Python 1 2 3 4 5 6 7 8 9 10 n = int(input()) lists = [] for i in range(n): lists.append(list(map(str, input().strip().split()))) lists[i][0] = int(lists[i][0]) lists.sort(key= lambda x:x[0]) # 0 인덱스를 오름차순으로, 만약 내림차순으로 하고 싶다면 -를 붙인다. / 그리고 0 인덱스가 동일하고 1을 내림으로 하고 싶다면 x:(x[0],-x[1]) for i in lists: print(i[0], i[1])
[백준] 방학 숙제 5532 Python (정렬) 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 ''' 방학 총일, 국어 총 페이지, 수학 총 페이지, 하루 최대 국어, 하루 최대 수학 ''' cnt1 = 1 cnt2 = 1 lists = [] for i in range(5): lists.append(int(input())) temp1 = lists[3] temp2 = lists[4] while lists[1] > lists[3]: cnt1 += 1 lists[3] += temp1 while lists[2] > lists[4]: cnt2 += 1 lists[4] += temp2 # print("cnt1: ", cnt1, "lists[3]: ", lists[3]) # print(..
[백준] 로또 6603 Python 1 2 3 4 5 6 7 8 9 10 11 12 import itertools while True: a, *b = map(int, input().strip().split()) if a == 0: break if a != 0: c = itertools.combinations(b,6) for i in c: for j in i: print(j, end=" ") print("") print("") 가변적인 길이의 input을 리스트로 받기 위한 *를 이용 (개꿀)
[백준] 괄호 9012 Python 1 2 3 4 5 6 7 8 9 N = int(input()) for i in range(N): temp = input() while "()" in temp: temp = temp.replace("()", "") if len(temp) > 0: print("NO") else: print("YES") Stack으로 풀 수 있지만 replace를 이용하여 제거 해나가는 식으로 가능함.
[백준] 하노이 탑 이동 순서 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)
[백준] 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