본문 바로가기

Algorithm

(42)
[백준] 최대공약수와 최소공배수 2609 Python C++ (1934 최소공배수 가능) Python 12345678910111213141516# 최대 공약수 - 유클리드# 최소공배수 - 두수의 곱 / 최대공약수def gcd(a, b): mod = a%b while mod != 0: a = b b = mod mod = a%b return b def lcm(a, b): return (a*b) // gcd(a,b) a, b = map(int, input().strip().split()) print(gcd(a,b))print(lcm(a,b))cs C++ 1234567891011121314151617181920212223242526272829#include using namespace std; int gcd(int a, int b){ int mod = a%b; while (mod != 0){ a =..
[백준] 수들의합 1789 Python C++ Python 123456789101112131415161718#include using namespace std; int main() { int i = 0; long sum = 0; long N = 0; scanf("%ld", &N); while (sum
[백준] 소인수분해 11653 Python C++ Python 1 2 3 4 5 6 7 8 9 N = int(input()) summ = 0 i = 0 while summ
[백준] 문자열 반복 2675 C++ 123456789101112131415161718192021222324252627282930#include #include #include using namespace std; int main() { string str_T; getline(cin, str_T); int int_T; int_T = stoi(str_T); int temp_range; string a; for (int i = 0; i
[백준] 화성수학 5355 C++ 1234567891011121314151617181920212223242526272829303132333435363738394041#include #include #include using namespace std; int main() { string N; int n; getline(cin, N); n = stoi(N); string s; float first; float *result = new float[n]; for (int i = 0; i
[백준] 스택수열 1874 Python 1234567891011121314151617181920212223N = int(input())obj = []stack = []result = []idx = 0 for i in range(N): obj.append(int(input())) for i in range(1, N+1): stack.append(i) result.append("+") while idx
이진탐색(binary search) N = int(input("입력하신 값의 위치를 출력하겠습니다. 입력해주세요: ")) a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] def binary(n): global a minn = 0 maxx = len(a)-1 cnt = 0 while True: mid = int((minn + maxx) / 2) cnt += 1 if a[mid] == n: break elif a[mid] > n: maxx = mid-1 elif a[mid]
하노이탑(재귀) Python cnt = 0 def hanoimove(num, fromm, by, to): global cnt cnt += 1 if num == 1: #print("%d이 %c에서 %c로 이동 " % (num, fromm, to)) print("%c -> %c" % (fromm, to)) else: hanoimove(num-1, fromm, to, by) #print("%d이 %c에서 %c로 이동 " % (num, fromm, to)) print("%c -> %c" % (fromm, to)) hanoimove(num-1, by, fromm, to) hanoimove(4,'A','B','C') print(cnt)