From b2c694bd1ac807d10c53c966ac1fdc3ca1d3543a Mon Sep 17 00:00:00 2001 From: hjyk0310 Date: Tue, 5 Apr 2022 02:51:35 +0900 Subject: [PATCH 1/2] 1st commit --- hannah/math/1110.py | 21 ++++++++ hannah/math/11653.py | 10 ++++ hannah/math/1934.py | 20 ++++++++ hannah/math/1978.py | 21 ++++++++ hannah/math/21919.py | 26 ++++++++++ hannah/math/21920.py | 25 ++++++++++ hannah/math/22864.py | 16 ++++++ hannah/math/2581.py | 27 ++++++++++ hannah/math/2609.py | 15 ++++++ hannah/math/2745.py | 38 ++++++++++++++ hannah/math/2960.py | 24 +++++++++ hannah/math/4134.py | 30 ++++++++++++ hannah/math/5347.py | 19 +++++++ hannah/math/5618.py | 23 +++++++++ hannah/math/9613.py | 25 ++++++++++ hannah/math/getPrimeNumbers.py | 49 +++++++++++++++++++ hannah/math/isPrimeNumber.py | 39 +++++++++++++++ ...4 \355\230\270\354\240\234\353\262\225.py" | 19 +++++++ 18 files changed, 447 insertions(+) create mode 100644 hannah/math/1110.py create mode 100644 hannah/math/11653.py create mode 100644 hannah/math/1934.py create mode 100644 hannah/math/1978.py create mode 100644 hannah/math/21919.py create mode 100644 hannah/math/21920.py create mode 100644 hannah/math/22864.py create mode 100644 hannah/math/2581.py create mode 100644 hannah/math/2609.py create mode 100644 hannah/math/2745.py create mode 100644 hannah/math/2960.py create mode 100644 hannah/math/4134.py create mode 100644 hannah/math/5347.py create mode 100644 hannah/math/5618.py create mode 100644 hannah/math/9613.py create mode 100644 hannah/math/getPrimeNumbers.py create mode 100644 hannah/math/isPrimeNumber.py create mode 100644 "hannah/math/\354\234\240\355\201\264\353\246\254\353\223\234 \355\230\270\354\240\234\353\262\225.py" diff --git a/hannah/math/1110.py b/hannah/math/1110.py new file mode 100644 index 0000000..e2e2dbf --- /dev/null +++ b/hannah/math/1110.py @@ -0,0 +1,21 @@ +# 더하기 사이클 + +count = 0 +def func(x): + + global count + + if x == n and count != 0: + return + + count += 1 + + if x < 10: + return func((x % 10)*10 + x) + else: + return func((x % 10)*10 + (x // 10 + x % 10) % 10) + +n = int(input()) + +func(n) +print(count) \ No newline at end of file diff --git a/hannah/math/11653.py b/hannah/math/11653.py new file mode 100644 index 0000000..00daea3 --- /dev/null +++ b/hannah/math/11653.py @@ -0,0 +1,10 @@ +# 소인수분해 + +n = int(input()) + +while n != 1: + for i in range(2,n + 1): + if n % i == 0: + n = n // i + print(i) + break \ No newline at end of file diff --git a/hannah/math/1934.py b/hannah/math/1934.py new file mode 100644 index 0000000..33d8cea --- /dev/null +++ b/hannah/math/1934.py @@ -0,0 +1,20 @@ +# 최소공배수 + +def gcd(a, b): + if b == 0: + return a + else: + return gcd(b, a % b) + +def lcm(a, b): + return a * b // gcd(a, b) + +t = int(input()) +tc = list() + +for i in range(t): + n, m = map(int, input().split()) + tc.append((n, m)) + +for i in range(t): + print(lcm(tc[i][0], tc[i][1])) \ No newline at end of file diff --git a/hannah/math/1978.py b/hannah/math/1978.py new file mode 100644 index 0000000..b528f93 --- /dev/null +++ b/hannah/math/1978.py @@ -0,0 +1,21 @@ +# 소수 찾기 +# 9:56~9:58 + +def isPrimeNumber(x): + if x == 1: + return 0 + + for i in range(2, x): # x == 2이면 for문 안돌고 바로 return True + if x % i == 0: + return 0 + return 1 + +n = int(input()) +inputs = list(map(int, input().split())) +count = 0 + +for i in inputs: + if isPrimeNumber(i) == 1: + count += 1 + +print(count) \ No newline at end of file diff --git a/hannah/math/21919.py b/hannah/math/21919.py new file mode 100644 index 0000000..a3e46d5 --- /dev/null +++ b/hannah/math/21919.py @@ -0,0 +1,26 @@ +# 소수 최소 공배수 +# isPrimeNumber의 for문 최적화 (제곱근까지로 변경) +# 백준에서 출력 초과가 뜸 -> 소수가 중복일 수 있다는 것 고려안해서 리스트를 set으로 감싸고 다시 list로 바꿔줘서 정답! + +import math + +def isPrimeNumber(x): + if x == 1 or x == 0: + return False + + for i in range(2, int(math.sqrt(x)) + 1): # x == 2이면 for문 안돌고 바로 return True + if x % i == 0: + return False + return True + +n = int(input()) +inputs = list(set(list(map(int, input().split())))) +result = 1 +for i in inputs: + if isPrimeNumber(i): + result *= i + +if result == 1: + print(-1) +else: + print(result) \ No newline at end of file diff --git a/hannah/math/21920.py b/hannah/math/21920.py new file mode 100644 index 0000000..ce7f78c --- /dev/null +++ b/hannah/math/21920.py @@ -0,0 +1,25 @@ +# 서로소 평균 +# 서로소 = 최대공약수가 1인 두 자연수 + +import sys +import math + +def isCoprime(x, y): + if math.gcd(x, y) == 1: + return True + else: + return False + +n = int(input()) +inputs = list(map(int, sys.stdin.readline().rstrip().split())) +x = int(input()) + +sum = 0 +count = 0 +for num in inputs: + if isCoprime(x, num): + sum += num + count += 1 + +avg = sum / count +print(avg) \ No newline at end of file diff --git a/hannah/math/22864.py b/hannah/math/22864.py new file mode 100644 index 0000000..00f215b --- /dev/null +++ b/hannah/math/22864.py @@ -0,0 +1,16 @@ +# 피로도 +a, b, c, m = map(int, input().split()) + +p = 0 +amountOfWork = 0 + +for i in range(24): + if p + a <= m: + p += a + amountOfWork += b + else: + p -= c + if p < 0: + p = 0 + +print(amountOfWork) \ No newline at end of file diff --git a/hannah/math/2581.py b/hannah/math/2581.py new file mode 100644 index 0000000..cb90409 --- /dev/null +++ b/hannah/math/2581.py @@ -0,0 +1,27 @@ +# 소수 +import math + +def isPrimeNumber(x): + if x == 1 or x == 0: + return False + + for i in range(2, math.sqrt(x) + 1): # x == 2이면 for문 안돌고 바로 return True + if x % i == 0: + return False + return True + +m = int(input()) +n = int(input()) +sum = 0 +min = 0 + +for i in range(n, m-1, -1): + if isPrimeNumber(i): # 만약 소수이면 + sum += i + min = i + +if sum == 0: + print(-1) +else: + print(sum) + print(min) \ No newline at end of file diff --git a/hannah/math/2609.py b/hannah/math/2609.py new file mode 100644 index 0000000..c7f4ec2 --- /dev/null +++ b/hannah/math/2609.py @@ -0,0 +1,15 @@ +# 최대공약수와 최소공배수 + +n, m = map(int, input().split()) + +def gcd(a, b): + if b == 0: + return a + else: + return gcd(b, a % b) + +def lcm(a, b): + return a * b // gcd(a, b) + +print(gcd(n, m)) +print(lcm(n, m)) \ No newline at end of file diff --git a/hannah/math/2745.py b/hannah/math/2745.py new file mode 100644 index 0000000..c2ea46a --- /dev/null +++ b/hannah/math/2745.py @@ -0,0 +1,38 @@ +# 진법 변환 +# 4:18~ + +# 공백없는 문자열 분리: 문자열을 그냥 리스트에 넣는다! list(string) 이렇게 +# 아스키코드 활용(알파벳 문자 <-> 숫자) 문자->숫자는 ord('A')이렇게, 참고: ord('A') == 65 + +# inputs = input().split() +# n = inputs[0] +# n_str = list(n) +# b = int(inputs[1]) +# result = 0 + +# for i in range(len(n_str), 0, -1): +# if type(n_str[i-1]) != type(1): # 각 자리수의 값이 숫자가 아닌 문자일 때 +# num = 10 + ord(n_str[i-1]) - ord('A') +# result += num * (b**(i-1)) +# else: +# print(int(n_str[i-1])) +# result += int(n_str[i-1]) + +# print(result) + +# inputs = input().split() +# n_str = list(inputs[0]) +# n_str.reverse() +# print(n_str) +# num = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ' +# b = int(inputs[1]) +# result = 0 + +# for i in range(len(n_str)-1, -1, -1): +# result += num.index(n_str[i]) * (b**i) + +# print(result) +# 틀렸습니다.... 왜 틀렸을까 + +n, b = input().split() +print(int(n, int(b))) \ No newline at end of file diff --git a/hannah/math/2960.py b/hannah/math/2960.py new file mode 100644 index 0000000..7d043eb --- /dev/null +++ b/hannah/math/2960.py @@ -0,0 +1,24 @@ +# 에라토스테네스의 체 +# N보다 작거나 같은 모든 소수를 찾는 알고리즘 +# 1. 2부터 N까지 모든 정수를 적는다. +# 2. 아직 지우지 않은 수 중 가장 작은 수를 찾는다. 이것을 P라고 하고, 이 수는 소수이다. +# 3. P를 지우고, 아직 지우지 않은 P의 배수를 크기 순서대로 지운다. +# 4. 아직 모든 수를 지우지 않았다면, 다시 2번 단계로 간다. +# 4:59~ + +n, k = map(int, input().split()) + +sieve = [True] * (n+1) # 체를 영어로 sieve라 한다! +# 모든 원소를 True로 일단 초기화해두고, 지우면 False로 바꾼다. +# 이 문제에서 0, 1은 소수는 아니지만 True로 두어도 ㄱㅊ... +# 어차피 for문에서 2부터 체크하고, 접근할 일이 없기 때문! + +count = 0 +for i in range(2, n+1): + if sieve[i] != False: + for j in range(i, n+1, i): # i부터 n+1까지 step을 i로 해서 반복 + if sieve[j] != False: + sieve[j] = False + count += 1 + if count == k: + print(j) \ No newline at end of file diff --git a/hannah/math/4134.py b/hannah/math/4134.py new file mode 100644 index 0000000..497c756 --- /dev/null +++ b/hannah/math/4134.py @@ -0,0 +1,30 @@ +# 다음 소수 + +import sys +import math + +def isPrimeNumber(x): + if x == 1 or x == 0: + return False + + for i in range(2, int(math.sqrt(x)) + 1): # x == 2이면 for문 안돌고 바로 return True + if x % i == 0: + return False + return True + +tc_count = int(input()) +result = list() + +for i in range(tc_count): + j = int(sys.stdin.readline().rstrip()) + + while 1: + if isPrimeNumber(j): + result.append(j) + break + else: + j += 1 + + +for i in range(len(result)): + print(result[i]) \ No newline at end of file diff --git a/hannah/math/5347.py b/hannah/math/5347.py new file mode 100644 index 0000000..0a268ed --- /dev/null +++ b/hannah/math/5347.py @@ -0,0 +1,19 @@ +# LCM +def gcd(a, b): + if b == 0: + return a + else: + return gcd(b, a % b) + +def lcm(a, b): + return a * b // gcd(a, b) + +n = int(input()) +results = list() + +for i in range(n): + a, b = map(int, input().split()) + results.append(lcm(a, b)) + +for i in results: + print(i) \ No newline at end of file diff --git a/hannah/math/5618.py b/hannah/math/5618.py new file mode 100644 index 0000000..09cc7d1 --- /dev/null +++ b/hannah/math/5618.py @@ -0,0 +1,23 @@ +# 공약수 +# 5:43~ +# 유클리드 호제법: 두 정수 a와 b(a > b)가 있을 때, a = bq + r (0 <= r < b)라 하면, +# gcd(a, b) = gcd(b, r)이 된다. (a와 b의 최대공약수 = b와 a % b의 최대공약수) +# 만약 r이 0 이면, a, b의 최대공약수는 b (식으로 표현하면 a % b = 0 이면 gcd(a, b) = b) +# gcd: greatest common divisor + +def gcd(a, b): + if b == 0: + return a + else: + return gcd(b, a % b) + +n = int(input()) +inputs = list(map(int, input().split())) + +gcd_value = gcd(inputs[0], gcd(inputs[1], inputs[-1])) # 인덱스 -1는 리스트의 마지막을 의미 + +for i in range(1, gcd_value // 2 + 1): + if gcd_value % i == 0: + print(i, end='\n') + +print(gcd_value) \ No newline at end of file diff --git a/hannah/math/9613.py b/hannah/math/9613.py new file mode 100644 index 0000000..1db8d4b --- /dev/null +++ b/hannah/math/9613.py @@ -0,0 +1,25 @@ +# GCD 합 + +from itertools import combinations +import sys +import math + +input = sys.stdin.readline +result = list() + +t = int(input()) + +for i in range(t): + sum = 0 + numbers = list(map(int, input().split())) + del numbers[0] + + for x, y in combinations(numbers, 2): + # 이런 방식의 for문은 2차원 리스트에서 안쪽 리스트의 크기가 모두 같고, 그 크기가 작을 때 사용한다. for과 in 사이에 쓰는 변수의 개수는 안쪽 리스트의 크기이다. + gcd = math.gcd(x, y) + sum += gcd + + result.append(sum) + +for i in range(t): + print(result[i]) \ No newline at end of file diff --git a/hannah/math/getPrimeNumbers.py b/hannah/math/getPrimeNumbers.py new file mode 100644 index 0000000..08044c8 --- /dev/null +++ b/hannah/math/getPrimeNumbers.py @@ -0,0 +1,49 @@ +# 에라토스테네스의 체: n보다 작거나 같은 모든 소수를 찾는 알고리즘 +# 범위 안에서 존재하는 모든 소수를 찾아야 하는 경우 사용 + +# 에라토스테네스의 체는 가장 먼저 소수를 판별할 범위만큼 리스트를 초기화하여, 해당하는 값을 넣어주고, 이후에 하나씩 지워나가는 방법을 이용한다. +# 1. 리스트를 생성하여 초기화한다. +# 2. 2부터 시작해서 특정 수의 배수에 해당하는 수를 모두 지운다.(지울 때 자기자신은 지우지 않고, 이미 지워진 수는 건너뛴다.) +# 3. 2부터 시작하여 남아있는 수를 모두 출력한다. + + +def getPrimeNumbers(n): # n 이하의 모든 소수를 리스트로 반환 + primes = list() # 소수를 따로 저장할 리스트 + sieve = [True] * (n+1) + + for i in range(2, n + 1): + if sieve[i] != False: + primes.append(i) + for j in range(i * 2, n + 1, i): + sieve[j] = False + return primes + +n = int(input()) + +print(getPrimeNumbers(n)) + + + + + + + + + +# getPrimeNumbers()의 주석추가 version +def getPrimeNumbers_descriptions(n): # 위 코드 설명! + primes = list() # 소수를 따로 저장할 리스트 + sieve = [True] * (n+1) + + for i in range(2, n + 1): + if sieve[i] != False: # sieve[i] == True면 소수리스트에 추가, sieve[i]가 True라면 이미 i는 2부터 (i-1)까지의 수들의 배수가 아닌 수이므로 그 수는 소수이다. i의 배수들은 이제 True에서 False로 바뀌고, 그 다음 True인 수는 무조건 소수일 것이다.. 이렇게 이해하면 되나 + primes.append(i) + for j in range(i * 2, n + 1, i): # i의 배수들을 False로 바꿔주는데, i는 소수니까 True를 유지해야하므로 i * 2부터 False로 바꿔줌.. 근데 사실 True/False 여부로 소수를 확인하는 것이 아니라 primes라는 리스트에 따로 저장해주므로 i라고 해도 상관없다.(i도 False로 바꿔줘도 상관 없다) + sieve[j] = False + return primes + +sieve_description = [True] * (n+1) # 체를 영어로 sieve라 한다! +# sieve = [False, False] + ([True] * (n - 1)) +# # 모든 원소를 True로 일단 초기화해두고, 지우면 False로 바꾼다. +# # 0, 1은 소수는 아니지만 True로 두어도 ㄱㅊ... +# # 어차피 for문에서 2부터 체크하고, 접근할 일이 없기 때문! diff --git a/hannah/math/isPrimeNumber.py b/hannah/math/isPrimeNumber.py new file mode 100644 index 0000000..a4c4eca --- /dev/null +++ b/hannah/math/isPrimeNumber.py @@ -0,0 +1,39 @@ +# 소수 판별 - 하나의 수가 소수인지 아닌지 판별해야 하는 경우 + +# 만약 수의 범위가 주어졌을 때, 범위 안에서 존재하는 모든 소수를 찾아야 하는 경우 isPrimeNumber() 대신 에라토스테네스의 체를 활용한 getPrimeNumbers를 활용하자 + +import math + +def isPrimeNumber(x): + if x == 1 or x == 0: + return False + + for i in range(2, int(math.sqrt(x)) + 1): # x == 2이면 for문 안돌고 바로 return True + if x % i == 0: + return False + return True + + +# for문에서 2부터 x까지가 아닌 2부터 int(math.sqrt(x)) + 1 까지로 해준 이유 +# 이코테 p.467 +# 어떤 수 x의 약수들을 나열했을 때, +# x가 소수가 아니라면 중간에 오는 약수는 제곱근이거나 제곱근 근처이다 +# 예1) 16의 경우, +# 1 * 16 = 16 +# 2 * 8 = 16 +# 4 * 4 = 16 +# 8 * 2 = 16 +# 16 * 1 = 16 + +# 가운데 약수 = 4 = 16의 제곱근, 가운데 약수 이후로는 대칭 + +# 따라서 4까지만 확인하면 됨 + +# 예2) 8의 경우, +# 1 * 8 = 8 +# 2 * 4 = 8 +# 4 * 2 = 8 +# 8 * 1 = 8 + +# 8의 제곱근은 2.x이므로 2까지만 확인하면 약수 절반 확인할 수 있음! 마찬가지로 나머지 절반은 대칭 +# for문에 쓸 range함수에는 정수가 들어가야 하므로 제곱근이 소수점일 경우를 대비해 int(math.sqrt(x)) + 1 이렇게 버림 하고 정수가 되도록 함 \ No newline at end of file diff --git "a/hannah/math/\354\234\240\355\201\264\353\246\254\353\223\234 \355\230\270\354\240\234\353\262\225.py" "b/hannah/math/\354\234\240\355\201\264\353\246\254\353\223\234 \355\230\270\354\240\234\353\262\225.py" new file mode 100644 index 0000000..f69d855 --- /dev/null +++ "b/hannah/math/\354\234\240\355\201\264\353\246\254\353\223\234 \355\230\270\354\240\234\353\262\225.py" @@ -0,0 +1,19 @@ +# 최대공약수 = gcd = greatest common divisor +# 최소공배수 = lcm = least common multiple + +# 유클리드 호제법: 두 정수 a와 b(a > b)가 있을 때, a = bq + r (0 <= r < b)라 하면, +# gcd(a, b) = gcd(b, r)이 된다. (a와 b의 최대공약수 = b와 a % b의 최대공약수) +# 만약 r이 0 이면, a, b의 최대공약수는 b (식으로 표현하면 a % b = 0 이면 gcd(a, b) = b) +# a, b의 최소 공배수는 a * b를 a, b의 최대공약수로 나눈 몫이다. +# 식으로 표현하면 lcm(a, b) = a * b // gcd(a, b) + +# 5618번, 2609번 + +def gcd(a, b): + if b == 0: + return a + else: + return gcd(b, a % b) + +def lcm(a, b): + return a * b // gcd(a, b) \ No newline at end of file From a89983bf18a7fca688944411499f7b88d103dd58 Mon Sep 17 00:00:00 2001 From: hjyk0310 Date: Thu, 14 Apr 2022 20:36:53 +0900 Subject: [PATCH 2/2] 2nd commit --- hannah/math/1747.py | 27 ++++++++++++++ hannah/math/21275.py | 52 +++++++++++++++++++++++++++ hannah/math/22943 (copy).py | 71 +++++++++++++++++++++++++++++++++++++ hannah/math/22943.py | 63 ++++++++++++++++++++++++++++++++ hannah/math/2745.py | 42 +++++++--------------- hannah/math/9613.py | 2 +- hannah/string/10798.py | 20 +++++++++++ hannah/string/11365.py | 11 ++++++ hannah/string/11720.py | 7 ++++ hannah/string/1181.py | 22 ++++++++++++ hannah/string/1316.py | 24 +++++++++++++ hannah/string/1316_2.py | 15 ++++++++ hannah/string/16171.py | 13 +++++++ hannah/string/16916.py | 14 ++++++++ hannah/string/17413.py | 34 ++++++++++++++++++ hannah/string/17609.py | 36 +++++++++++++++++++ hannah/string/17609_0.py | 23 ++++++++++++ hannah/string/1764.py | 22 ++++++++++++ hannah/string/20154.py | 15 ++++++++ hannah/string/20291.py | 21 +++++++++++ hannah/string/3029.py | 20 +++++++++++ hannah/string/4659.py | 40 +++++++++++++++++++++ hannah/string/6550.py | 23 ++++++++++++ hannah/string/9046.py | 23 ++++++++++++ hannah/string/9342.py | 14 ++++++++ 25 files changed, 623 insertions(+), 31 deletions(-) create mode 100644 hannah/math/1747.py create mode 100644 hannah/math/21275.py create mode 100644 hannah/math/22943 (copy).py create mode 100644 hannah/math/22943.py create mode 100644 hannah/string/10798.py create mode 100644 hannah/string/11365.py create mode 100644 hannah/string/11720.py create mode 100644 hannah/string/1181.py create mode 100644 hannah/string/1316.py create mode 100644 hannah/string/1316_2.py create mode 100644 hannah/string/16171.py create mode 100644 hannah/string/16916.py create mode 100644 hannah/string/17413.py create mode 100644 hannah/string/17609.py create mode 100644 hannah/string/17609_0.py create mode 100644 hannah/string/1764.py create mode 100644 hannah/string/20154.py create mode 100644 hannah/string/20291.py create mode 100644 hannah/string/3029.py create mode 100644 hannah/string/4659.py create mode 100644 hannah/string/6550.py create mode 100644 hannah/string/9046.py create mode 100644 hannah/string/9342.py diff --git a/hannah/math/1747.py b/hannah/math/1747.py new file mode 100644 index 0000000..7ec674c --- /dev/null +++ b/hannah/math/1747.py @@ -0,0 +1,27 @@ +# 소수&팰린드롬 + +import math +import sys + +def isPrimeNumber(x): + if x == 1 or x == 0: + return False + + for i in range(2, int(math.sqrt(x)) + 1): # x == 2이면 for문 안돌고 바로 return True + if x % i == 0: + return False + return True + +def isPalindrome(x): + if x == x[::-1]: + return True + +input = sys.stdin.readline + +n = int(input()) + +while 1: + if isPrimeNumber(n) and isPalindrome(str(n)): + print(n) + break + n += 1 \ No newline at end of file diff --git a/hannah/math/21275.py b/hannah/math/21275.py new file mode 100644 index 0000000..ecf2f3a --- /dev/null +++ b/hannah/math/21275.py @@ -0,0 +1,52 @@ +# 폰 호석만 +# 4:57~ +import time +start = time.time() + +def transform(x, n): + result = 0 + for i in range(len(x)): + if ord(x[i]) >= ord('a'): + result += (ord(x[i]) - ord('a') + 10) * n**(len(x) - 1 - i) + else: + result += int(x[i])*n**(len(x) - 1 - i) + return result + +Xa, Xb = input().split() + +alph = '0123456789abcdefghijklmnopqrstuvwxyz' +count = 0 +X, A, B = -1, 0, 0 + +max_Xa, max_Xb = 2, 2 +for i in range(len(Xa)): + max_Xa = max(max_Xa, alph.find(Xa[i])+1) + # 10(a)진법이면 0~9까지 쓰니까 + # 예를들어 p가 최대값이면, (p+1)진법부터 가능성 있음 + +for i in range(len(Xb)): + max_Xb = max(max_Xb, alph.find(Xb[i])+1) + +# print("max_Xa = ", max_Xa) +# print("max_Xb = ", max_Xb) + + +for a in range(max_Xa, 37): + for b in range(max_Xb, 37): + temp_Xa = transform(Xa, a) + if a == b or temp_Xa >= 2**63: # 주의: 문제 제한 조건으로 A != B, 0 <= X < 2**63 이 있음 + break + if temp_Xa == transform(Xb, b): + A, B = a, b + X = temp_Xa + count += 1 + # print("A = ", A, ", B = ", B, ", X = ", X, ", count = ", count) + +if count > 1 or X == 0: + print("Multiple") +elif count == 1: + print(X, " ", A, " ", B) +else: + print("Impossible") + +print("time :", time.time() - start) \ No newline at end of file diff --git a/hannah/math/22943 (copy).py b/hannah/math/22943 (copy).py new file mode 100644 index 0000000..e058892 --- /dev/null +++ b/hannah/math/22943 (copy).py @@ -0,0 +1,71 @@ +# 수 + +import sys +import math +import itertools + +def getPrimeNumbers(k): # k로 정의되는 범위 내의 모든 소수를 리스트로 반환 + numbers = list(num_range) + start, end = numbers[0], numbers[-1]+1 + if start == 1: + start = 2 + print("start = ", start, ", end = ", end) + primes = list() # 소수를 따로 저장할 리스트 + sieve = [True] * end + + for i in range(start, end): + if sieve[i] != False: + primes.append(i) + for j in range(i * 2, end, i): + sieve[j] = False + return primes + +input = sys.stdin.readline + +k, m = map(int, input().split()) + +num_range = range(10**(k-1), 10**k) + +result = [False for x in num_range] + +primes = getPrimeNumbers(k) +print("primes = ", primes) + + + +def isSumOfDifferentPrimeNumber(x): # 서로 다른 두 개의 소수의 합인지 + print(x, ": isSum~ start") + # 이중 for문, x까지 모든 소수 + numbers1 = primes + for i in numbers1: + for j in numbers1: + if i == j: + + continue + if i + j == x: + print("True: x = ", x, ", i = ", i, ", j = ", j) + return True + print("False: x = ", x, ", i = ", i, ", j = ", j) + return False + +def isSatisfied2ndCondition(x, m): + num = x % m + primes = getPrimeNumbers(x) + for i in primes: + for j in primes: + if num == i * j: + + return True + return False +print("result = ", result) +for i in num_range: + if isSumOfDifferentPrimeNumber(i) == False: + print("조건1: removed number = ", i) + result[i] = False +print(result) +# for num in num_range: +# if isSatisfied2ndCondition(num, m) == False: +# result.remove(num) + +# print(result) + \ No newline at end of file diff --git a/hannah/math/22943.py b/hannah/math/22943.py new file mode 100644 index 0000000..16be8d1 --- /dev/null +++ b/hannah/math/22943.py @@ -0,0 +1,63 @@ +# 수 + +import sys +import time + +def getPrimeNumbers(k): # k로 정의되는 범위 내의 모든 소수를 리스트로 반환 + numbers = list(num_range) + start, end = numbers[0], numbers[-1]+1 + if start == 1: + start = 2 + primes = list() # 소수를 따로 저장할 리스트 + sieve = [True] * end + + for i in range(start, end): + if sieve[i] != False: + primes.append(i) + for j in range(i * 2, end, i): + sieve[j] = False + return primes + +input = sys.stdin.readline + +k, m = map(int, input().split()) + +num_range = range(10**(k-1), 10**k) + +result = [False for x in range(10**k)] + +primes = getPrimeNumbers(k) +# print("primes = ", primes) + +def isSumOfDifferentPrimeNumber(x): # 서로 다른 두 개의 소수의 합인지 + # 이중 for문, x까지 모든 소수 + numbers1 = primes + for i in numbers1: + for j in numbers1: + if i == j: + + continue + if i + j == x: + # print("True: x = ", x, ", i = ", i, ", j = ", j) + return True + # print("False: x = ", x, ", i = ", i, ", j = ", j) + return False + +def isSatisfied2ndCondition(x, m): + num = x % m + primes = getPrimeNumbers(x) + for i in primes: + for j in primes: + if num == i * j: + return True + return False +# print("result = ", result) + +for i in num_range: + if isSumOfDifferentPrimeNumber(i): + result[i] = True + if isSatisfied2ndCondition(i, m) == False: + result[i] = False + +# print("조건2 끝 result: ", result) +print(result.count(True)) \ No newline at end of file diff --git a/hannah/math/2745.py b/hannah/math/2745.py index c2ea46a..33de4a3 100644 --- a/hannah/math/2745.py +++ b/hannah/math/2745.py @@ -4,35 +4,17 @@ # 공백없는 문자열 분리: 문자열을 그냥 리스트에 넣는다! list(string) 이렇게 # 아스키코드 활용(알파벳 문자 <-> 숫자) 문자->숫자는 ord('A')이렇게, 참고: ord('A') == 65 -# inputs = input().split() -# n = inputs[0] -# n_str = list(n) -# b = int(inputs[1]) -# result = 0 - -# for i in range(len(n_str), 0, -1): -# if type(n_str[i-1]) != type(1): # 각 자리수의 값이 숫자가 아닌 문자일 때 -# num = 10 + ord(n_str[i-1]) - ord('A') -# result += num * (b**(i-1)) -# else: -# print(int(n_str[i-1])) -# result += int(n_str[i-1]) - -# print(result) - -# inputs = input().split() -# n_str = list(inputs[0]) -# n_str.reverse() -# print(n_str) -# num = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ' -# b = int(inputs[1]) -# result = 0 +n, b = input().split() +b = int(b) -# for i in range(len(n_str)-1, -1, -1): -# result += num.index(n_str[i]) * (b**i) - -# print(result) -# 틀렸습니다.... 왜 틀렸을까 +result = 0 +for i in range(len(n)): + if ord(n[i]) >= 65: + result += (ord(n[i]) - 65 + 10) * b**(len(n) - 1 - i) + else: + result += int(n[i])*b**(len(n) - 1 - i) +print(result) -n, b = input().split() -print(int(n, int(b))) \ No newline at end of file +# 라이브러리 써서 쉽게 풀기 - 위 코드보다 24ms 정도 느림 +# n, b = input().split() +# print(int(n, int(b))) \ No newline at end of file diff --git a/hannah/math/9613.py b/hannah/math/9613.py index 1db8d4b..b90fec1 100644 --- a/hannah/math/9613.py +++ b/hannah/math/9613.py @@ -15,7 +15,7 @@ del numbers[0] for x, y in combinations(numbers, 2): - # 이런 방식의 for문은 2차원 리스트에서 안쪽 리스트의 크기가 모두 같고, 그 크기가 작을 때 사용한다. for과 in 사이에 쓰는 변수의 개수는 안쪽 리스트의 크기이다. + # 이런 방식의 for문은 2차원 리스트에서 안쪽 리스트의 크기가 모두 같고, 그 크기가 작을 때 사용한다. for과 in 사이에 쓰는 변수는 안쪽 리스트의 크기가 n이면 n개 써준다. gcd = math.gcd(x, y) sum += gcd diff --git a/hannah/string/10798.py b/hannah/string/10798.py new file mode 100644 index 0000000..e0cf9b4 --- /dev/null +++ b/hannah/string/10798.py @@ -0,0 +1,20 @@ +# 세로읽기 +import sys + +input = sys.stdin.readline +inputs = list() +result = "" + +for i in range(5): + inputs.append(input().rstrip()) + +max_length = len(max(inputs, key = len)) + +for j in range(max_length): + t_string = "" + for i in range(5): + if (len(inputs[i]) - 1) >= j: # inputs[i] 문자열의 마지막 인덱스가 j보다 작으면 pass + t_string += inputs[i][j] + result += t_string + +print(result) \ No newline at end of file diff --git a/hannah/string/11365.py b/hannah/string/11365.py new file mode 100644 index 0000000..62023e5 --- /dev/null +++ b/hannah/string/11365.py @@ -0,0 +1,11 @@ +# !밀비 급일 + +import sys + +input = sys.stdin.readline + +while True: + inputString = input().rstrip() + if inputString == "END": + break + print(inputString[::-1]) \ No newline at end of file diff --git a/hannah/string/11720.py b/hannah/string/11720.py new file mode 100644 index 0000000..e3e0f58 --- /dev/null +++ b/hannah/string/11720.py @@ -0,0 +1,7 @@ +# 숫자의 합 +# 6:22~26 + +n = int(input()) +inputs = list(input()) +numbers = list(map(int, inputs)) +print(sum(numbers)) \ No newline at end of file diff --git a/hannah/string/1181.py b/hannah/string/1181.py new file mode 100644 index 0000000..8c4eb7b --- /dev/null +++ b/hannah/string/1181.py @@ -0,0 +1,22 @@ +# 단어 정렬 + +import sys + +input = sys.stdin.readline + +n = int(input()) +words = list() + +for _ in range(n): + word = input().rstrip() + words.append(word) + +words = list(set(words)) #중복 제거 + +# 알파벳 순 정렬 후 길이순 정렬을 해야 길이가 같을 때 알파벳 순 정렬이 된다. +# 반대로 길이순 정렬을 하고 알파벳 순 정렬을 하면 그냥 알파벳 순 정렬이 될 것이다! +words.sort() +words.sort(key = len) + +for word in words: + print(word) \ No newline at end of file diff --git a/hannah/string/1316.py b/hannah/string/1316.py new file mode 100644 index 0000000..46cd0c2 --- /dev/null +++ b/hannah/string/1316.py @@ -0,0 +1,24 @@ +# 그룹 단어 체커 + +import sys + +def isGroupWord(word): + alph = list() + for i in range(len(word)): + temp_alph = word[i] + if temp_alph not in alph: # 지금까지 확인한 알파벳 리스트에 없으면 추가 + alph.append(temp_alph) + else: # 리스트에 있으면, + if temp_alph != word[i-1]: # 리스트에 있는데 이전 인덱스의 단어와 일치하지 않으면 그룹단어가 아님 + return False + return True + +input = sys.stdin.readline + +n = int(input()) +count = 0 +for _ in range(n): + if isGroupWord(input().rstrip()): + count += 1 + +print(count) \ No newline at end of file diff --git a/hannah/string/1316_2.py b/hannah/string/1316_2.py new file mode 100644 index 0000000..9d48525 --- /dev/null +++ b/hannah/string/1316_2.py @@ -0,0 +1,15 @@ +# 그룹 단어 체커 - 다른 풀이 + +import sys + +input = sys.stdin.readline + +n = int(input()) + +count = 0 +for i in range(n): + word = input().rstrip() + if list(word) == sorted(word, key=word.find): # 기존 위치 그대로 같은 알파벳끼리 붙어있게 정렬(그룹단어 그 자체) + count += 1 + +print(count) \ No newline at end of file diff --git a/hannah/string/16171.py b/hannah/string/16171.py new file mode 100644 index 0000000..d856494 --- /dev/null +++ b/hannah/string/16171.py @@ -0,0 +1,13 @@ +# 나는 친구가 적다 (Small) + +str = input() +key = input() +new_str = str +for i in range(len(str)): + if str[i].isdigit(): + new_str = new_str.replace(str[i], "") + +if key in new_str: + print("1") +else: + print("0") \ No newline at end of file diff --git a/hannah/string/16916.py b/hannah/string/16916.py new file mode 100644 index 0000000..828d18a --- /dev/null +++ b/hannah/string/16916.py @@ -0,0 +1,14 @@ +# 부분 문자열 +# 시간 초과 - 수정 필요 + +import sys + +input = sys.stdin.readline + +s = input().rstrip() +p = input().rstrip() + +if p in s: + print("1") +else: + print("0") \ No newline at end of file diff --git a/hannah/string/17413.py b/hannah/string/17413.py new file mode 100644 index 0000000..c56bf2c --- /dev/null +++ b/hannah/string/17413.py @@ -0,0 +1,34 @@ +# 단어 뒤집기 2 + +str = input() +isTag = False # True면 제대로 출력, False면 뒤집어서 출력 +temp = str +result = '' + +if str[0] == "<": + isTag = True + +for i in range(len(str)): + if isTag == False: + if str[i] == " ": + idx = temp.index(" ") + result += temp[idx-1::-1] + " " + temp = temp[idx+1:] + elif str[i] == "<": + isTag = True + if str[i-1] == ">": # ">" 다음 바로 "<" 나온 경우 + continue + idx = temp.index("<") + result += temp[idx-1::-1] + temp = temp[idx:] + elif i == len(str) - 1: + result += temp[::-1] + else: + if str[i] == ">": + isTag = False + idx = temp.index(">") + result += temp[:idx+1] + temp = temp[idx+1:] + elif str[i] == "<": # ">" 다음 바로 "<" 나온 경우 + isTag = True +print(result) \ No newline at end of file diff --git a/hannah/string/17609.py b/hannah/string/17609.py new file mode 100644 index 0000000..b153da7 --- /dev/null +++ b/hannah/string/17609.py @@ -0,0 +1,36 @@ +# 회문 +# 투 포인터 적용 + +import sys + +# 투 포인터 이용하여 유사팰린드롬인지 확인 +def getResult(string): # 문자열리스트와 뒤집은 문자열리스트를 파라미터로 + if string == string[::-1]: + return 0 + + n = len(string) # string의 길이 = reversed string의 길이 + l, r = 0, n-1 + while l < r: + if string[l] != string[r]: + # l을 +1해서 회문 확인 + if string[l+1:r+1] == string[r:l:-1]: + return 1 + + # r을 -1해서 회문 확인 + if l == 0: # l == 0인 경우 string[r-1:l-1:-1] 할 때 두번째 인자가 -1이 되어서 null을 반환하므로 0일때를 따로 빼줌 + if string[l:r] == string[r-1::-1]: + return 1 + elif string[l:r] == string[r-1:l-1:-1]: + return 1 + return 2 + else: # l의 문자와 r의 문자가 같으면 l++, r--해줌 + l += 1 + r -= 1 + +input = sys.stdin.readline + +t = int(input()) + +for _ in range(t): + str = input().rstrip() + print(getResult(str)) \ No newline at end of file diff --git a/hannah/string/17609_0.py b/hannah/string/17609_0.py new file mode 100644 index 0000000..09f2b2a --- /dev/null +++ b/hannah/string/17609_0.py @@ -0,0 +1,23 @@ +# 회문 +# 시간 초과 -> 수정 필요 + +import sys + +def getResult(str): + if str == str[::-1]: + return "0" + else: + for i in range(len(str) // 2): + _str = list(str) + del _str[i] + if _str == _str[::-1]: + return "1" + return "2" + +input = sys.stdin.readline + +t = int(input()) + +for _ in range(t): + str = input().rstrip() + print(getResult(str)) \ No newline at end of file diff --git a/hannah/string/1764.py b/hannah/string/1764.py new file mode 100644 index 0000000..d2f5bee --- /dev/null +++ b/hannah/string/1764.py @@ -0,0 +1,22 @@ +# 듣보잡 + +import sys + +input = sys.stdin.readline + +n, m = map(int, input().split()) +deutmot = set() # 듣도 못한 사람 리스트 +bomot = set() # 보도 못한 사람 리스트 + +for _ in range(n): + deutmot.add(input().rstrip()) + +for _ in range(m): + bomot.add(input().rstrip()) + +deutbo = list(deutmot.intersection(bomot)) # 듣보 리스트 + +deutbo.sort() +print(len(deutbo)) +for x in deutbo: + print(x) \ No newline at end of file diff --git a/hannah/string/20154.py b/hannah/string/20154.py new file mode 100644 index 0000000..75d02cd --- /dev/null +++ b/hannah/string/20154.py @@ -0,0 +1,15 @@ +# 이 구역의 승자는 누구야? + +nums = [3, 2, 1, 2, 3, 3, 3, 3, 1, 1, 3, 1, 3, 3, 1, 2, 2, 2, 1, 2, 1 ,1, 2, 2, 2, 1] + +str = input() + +sum = 0 +for alph in str: + sum += nums[ord(alph) - ord('A')] + sum %= 10 + +if sum % 2 != 0: + print("I'm a winner!") +else: + print("You're the winner?") \ No newline at end of file diff --git a/hannah/string/20291.py b/hannah/string/20291.py new file mode 100644 index 0000000..8bb9276 --- /dev/null +++ b/hannah/string/20291.py @@ -0,0 +1,21 @@ +# 파일 정리 + +import sys + +input = sys.stdin.readline + +n = int(input()) + +result = {} +for _ in range(n): + ext = input().rstrip().split(".")[1] + if ext in result: + result[ext] += 1 + else: + result[ext] = 1 + +keys = list(result.keys()) +keys.sort() + +for key in keys: + print(key, result[key]) \ No newline at end of file diff --git a/hannah/string/3029.py b/hannah/string/3029.py new file mode 100644 index 0000000..e265550 --- /dev/null +++ b/hannah/string/3029.py @@ -0,0 +1,20 @@ +# 경고 + +import sys + +input = sys.stdin.readline + +h1, m1, s1 = map(int, input().split(":")) +h2, m2, s2 = map(int, input().split(":")) + +t1 = h1 * 60 * 60 + m1 * 60 + s1 +t2 = h2 * 60 * 60 + m2 * 60 + s2 +if t1 >= t2: + t = (24 * 60 * 60 - t1) + t2 +else: + t = t2 - t1 + +h = t // (60 * 60) +m = t % (60 * 60) // 60 +s = t % (60 * 60) % 60 +print("%02d:%02d:%02d" %(h, m, s)) \ No newline at end of file diff --git a/hannah/string/4659.py b/hannah/string/4659.py new file mode 100644 index 0000000..33210e3 --- /dev/null +++ b/hannah/string/4659.py @@ -0,0 +1,40 @@ +# 비밀번호 발음하기 + +import sys + +def isSatisfiedCondition(s): + cond1 = False + cond2 = True + cond3 = True + for x in s: + if x in vowel: + cond1 = True + break + for i in range(len(s) - 2): + if s[i] in vowel and s[i+1] in vowel and s[i+2] in vowel: + cond1 = False + break + if s[i] not in vowel and s[i+1] not in vowel and s[i+2] not in vowel: + cond2 = False + break + for i in range(len(s) - 1): + if s[i] == s[i+1]: + if (s[i] != 'o' or s[i+1] != 'o') and (s[i] != 'e' or s[i+1] != 'e'): + cond3 = False + break + if cond1 == True and cond2 == True and cond3 == True: + return True + else: + return False + +input = sys.stdin.readline +vowel = ['a', 'e', 'i', 'o', 'u'] + +while True: + str = input().rstrip() + if str == 'end': + break + if isSatisfiedCondition(str): + print(f'<{str}> is acceptable.') + else: + print(f'<{str}> is not acceptable.') \ No newline at end of file diff --git a/hannah/string/6550.py b/hannah/string/6550.py new file mode 100644 index 0000000..bd0e29d --- /dev/null +++ b/hannah/string/6550.py @@ -0,0 +1,23 @@ +# 부분 문자열 + +import sys + +input = sys.stdin.readline +while True: + try: + s, t = input().rstrip().split() + k = 0 + j = 0 + flag = 0 + for i in range(len(t)): + if s[j] == t[i]: + j += 1 + if j == len(s): + flag = 1 + break + if flag == 0: + print("No") + else: + print("Yes") + except: + break \ No newline at end of file diff --git a/hannah/string/9046.py b/hannah/string/9046.py new file mode 100644 index 0000000..6f2d88c --- /dev/null +++ b/hannah/string/9046.py @@ -0,0 +1,23 @@ +# 복호화 + +import sys + +input = sys.stdin.readline +t = int(input()) +input_string = "" + +alp = 'abcdefghijklmnopqrstuvwxyz' + +for i in range(t): + input_string = input().rstrip() + frequency = [0] * 26 + for alphabet in input_string: + try: + frequency[alp.index(alphabet)] += 1 + except: + continue + mf = max(frequency) + if frequency.count(mf) == 1: + print(alp[frequency.index(mf)]) + else: + print("?") \ No newline at end of file diff --git a/hannah/string/9342.py b/hannah/string/9342.py new file mode 100644 index 0000000..f3ffdd5 --- /dev/null +++ b/hannah/string/9342.py @@ -0,0 +1,14 @@ +# 염색체 + +import re, sys + +input = sys.stdin.readline +regex = re.compile('^[A-F]{0,1}A+F+C+[A-F]{0,1}$') +t = int(input()) + +for i in range(t): + str = input().rstrip() + if regex.match(str) != None: + print("Infected!") + else: + print("Good") \ No newline at end of file