# ITSA E-tutor Mathematics I Python Version {%hackmd BJrTq20hE %} ###### tags: `python` :::warning :warning:**Warining :** 1. If you have **NOT** read "[Python Basic Notes"](https://hackmd.io/@cheung4843/HycouTvtu) which is designed for the beginners, please read that first. 2. **Do Not merely take a glance at these codes, please try them hands-on.** 3. Some solutions are **NOT** optimal, however, they are relatively easy and Intuitive for the beginners. ::: :::info :blue_book: **The Method Of Use :** 1. Here are most solutions to [this problem database](https://e-tutor.itsa.org.tw/e-Tutor/mod/programming/index.php?id=284). Please register an account on e-tutor first, clicking [this](https://portal.itsa.org.tw/user/register). 2. If you have more interest on C++, please click [this](https://hackmd.io/@cheung4843/H1pi30DTr) to see the C++ version. ::: :::success :pencil: **Start Programming:** 1. [e-tutor](https://e-tutor.itsa.org.tw/e-Tutor/) 2. [repl.it](https://replit.com/languages/python3) ::: > [TOC] --- ## [C_MM01-易] 計算梯型面積 ```python= while True: a, b, c = map(float, input().split()) area = ((a + b) * c) / 2 print("Trapezoid area:" + str(area)) ``` ## [C_MM02-易] 計算三角形面積 ```python= while True: a, b = map(float, input().split()) area = (a * b) / 2 print(area) ``` ## [C_MM03-易] 兩數總和 ```python= while True: a, b = map(int, input().split()) print(a + b) ``` ## [C_MM04-易] 計算總和、乘積、差、商和餘數 ```python= while True: a, b = map(int, input().split()) print('{}+{}={}'.format(a, b, a + b)) print('{}*{}={}'.format(a, b, a * b)) print('{}-{}={}'.format(a, b, a - b)) print('{}/{}={}...{}'.format(a, b, a // b, a % b)) ``` ## [C_MM05-易] 計算正方形面積 ```python= from decimal import Decimal, ROUND_HALF_UP while True: a = input() print((Decimal(a) ** Decimal(2)).quantize(Decimal('0.0'), rounding=ROUND_HALF_UP)) ``` ## [C_MM06-易] 英哩轉公里 ```python= while True: mi = int(input()) km = mi * 1.6 print('{0:.1f}'.format(km)) ``` ## [C_MM07-易] 計算平方值與立方值 ```python= while True: a = int(input()) print(a, a**2, a**3) ``` ## [C_MM08-易] 計算兩數和的平方值 ```python= while True: a, b = map(int, input().split()) print((a + b) ** 2) ``` ## [C_MM09-易] 計算 i 次方的值 ```python= while True: i = int(input()) if i > 31: print('Value of more than 31') else: print(1 << i) ``` ## [C_MM10-易] 攝氏溫度轉華式溫度 ```python= while True: c = float(input()) f = c * 1.8 + 32 print('{0:.1f}'.format(f)) ``` ## [C_MM11-易] 購票計算 ```python= while True: x = int(input()) print('NT10={}'.format(x // 10)) x %= 10 print('NT5={}'.format(x // 5)) x %= 5 print('NT1={}'.format(x)) ``` ## [C_MM12-易] 相遇時間計算 ```python= from math import ceil while True: d = int(input()) t = d * 100 / (100 - 30 * 2.54) print(ceil(t)) ``` ## [C_MM13-易] 停車費計算 ```python= while True: h1, m1 = map(int, input().split()) h2, m2 = map(int, input().split()) s = 0 t = 60 * (h2 - h1) + (m2 - m1) if t > 240: s += ((t - 240) // 30) * 60 t = 240 if t > 120: s += ((t - 120) // 30) * 40 t = 120 s += (t // 30) * 30 print(s) ``` ## [C_MM14-易] 計算時間的組合 ```python= while True: s = int(input()) print('{} days'.format(s // 86400)) print('{} hours'.format(s % 86400 // 3600)) print('{} minutes'.format(s % 3600 // 60)) print('{} seconds'.format(s % 60)) ``` ## [C_MM15-易] 判斷座標是否在正方形的範圍內 ```python= while True: x, y = map(int, input().split()) if 0 <= x <= 100 and 0 <= y <= 100: print('inside') else: print('outside') ``` ## [C_MM16-易] 判斷座標是否在圓形的範圍內 ```python= while True: x, y = map(int, input().split()) if x ** 2 <= 10000 and y ** 2 <= 10000: print('inside') else: print('outside') ``` ## [C_MM17-易] 求最大公因數 ```python= from math import gcd while True: x, y = map(int, input().split()) print(gcd(x, y)) ``` ## [C_MM18-易] 十進制轉二進制 ```python= byte = [1] * 8 while True: x = int(input()) isPos = x > 0 if not isPos: x = ~x for i in range(7, -1, -1): byte[i] = x % 2 x //= 2 if not isPos: for i in range(0, 8): byte[i] ^= 1 for bit in byte: print(bit, end='') print() ``` ## [C_MM19-易] 電話費計算 ```python= while True: m = int(input()) s = m * 0.9 if m >= 1500: s *= 0.79 elif m > 800: s *= 0.9 print('{0:.1f}'.format(s)) ``` ## [C_MM20-易] 十進位轉十六進位 ```python= while True: x = int(input()) print(format(x, 'X')) ``` ## [C_MM21-易] 算階乘 ```python= from math import factorial while True: x = int(input()) print(factorial(x)) ``` ## [C_MM24-易] 計算薪水 ```python= while True: h, p = map(int, input().split()) if h <= 60: s = h * p elif 60 < h < 121: s = (60 * p) + (h - 60) * p * 1.33 elif h >= 121: s = (60 * p * 2.33) + (h - 120) * p * 1.66 print('{0:.1f}'.format(s)) ``` ## [C_MM25-易] 計算正整數被3整除之數值之總和 ```python= while True: N = int(input()) s = 0 for i in range(0, N + 1, 3): s += i print(s) ``` ## [C_MM26-易] 輸出 1x1、2x2、...、NxN之結果 ```python= while True: N = int(input()) for i in range(1, N + 1): print('{}*{}={}'.format(i, i, i * i)) ``` ## [C_MM27-易] 計算兩整數間所有整數的總和 ```python= while True: a, b = map(int, input().split()) print(((a + b) * (abs(a - b) + 1)) // 2) ``` ## [C_MM28-易] 計算1到N之間屬於5和7的倍數 ```python= while True: N = int(input()) if N >= 35: print(35, end='') for i in range(70, N + 1, 35): print(' {}'.format(i), end='') print() ``` ## [C_MM29-易] 最大質數問題 此處採用$Bruteforce$。 ```python= def is_prime(x): if x > 1: for j in range(2, int(x ** (1 / 2)) + 1): if x % j == 0: return False return True else: return False while True: N = int(input()) for i in range(N - 1, 2, -1): if is_prime(i): print(i) break ``` ## [C_MM30-易] 質數判別 此處採用$Eratosthenes$,後台測資小於$2^{10}$,如果把表開到$2^{31}$,會TLE。 ```python= MAX = 2 ** 10 prime = [True] * MAX for i in range(2, int(MAX ** 0.5) + 1): if prime[i]: for j in range(i * i, MAX, i): prime[j] = False while True: N = int(input()) if prime[N]: print('YES') else: print('NO') ``` ## [C_MM31-易] 計算1~N內能被2跟3整除,但不能被12整除的整數總和 ```python= while True: N = int(input()) print(sum([i for i in range(0, N + 1, 6) if i % 6 == 0 and i % 12 != 0])) ``` ## [C_MM32-易] Armstrong數 ```python= while True: N = int(input()) if (N // 100) ** 3 + (N % 100 // 10) ** 3 + (N % 10) ** 3 == N: print('Yes') else: print('No') ``` ## [C_MM33-易] 找1~N的完美數 測資小於$2^{13}$,事實上,直接建好表會更快;這裡只是示範如何建表。 ```python= table = [] for i in range(6, 2 ** 13, 2): s = 0 for j in range(1, i): if i % j == 0: s += j if s == i: table.append(i) while True: N = int(input()) print(' '.join([str(x) for x in table if x <= N])) ``` ## [C_MM34-易] 因數問題 ```python= while True: N = int(input()) ans = [] for i in range(1, N + 1): if N % i == 0: ans.append(str(i)) print(' '.join(ans)) ``` ## [C_MM35-易] 平、閏年判定 ```python= while True: N = int(input()) if N % 400 == 0 or (N % 4 == 0 and N % 100 != 0): print('Bissextile Year') else: print('Common Year') ``` ## [C_MM36-易] 季節判定 ```python= while True: N = int(input()) if N in [3, 4, 5]: print('Spring') elif N in [6, 7, 8]: print('Summer') elif N in [9, 10, 11]: print('Autumn') elif N in [12, 1, 2]: print('Winter') ``` ## [C_MM37-易] 判斷座標位於何處 ```python= while True: x, y = map(int, input().split()) if x == 0 and y == 0: print('Origin') elif x != 0 and y == 0: print('x-axis') elif x == 0 and y != 0: print('y-axis') elif x > 0 and y > 0: print('1st Quadrant') elif x < 0 < y: print('2nd Quadrant') elif x < 0 and y < 0: print('3rd Quadrant') elif x > 0 > y: print('4th Quadrant') ``` ## [C_MM38-易] 判斷3整數是否能構成三角形之三邊長 ```python= while True: x, y, z = map(int, input().split()) if x + y > z and x + z > y and y + z > x: print('fit') else: print('unfit') ``` ## [C_MM39-易] 判斷是何種三角形 ```python= while True: a, b, c = map(int, input().split()) a2, b2, c2 = a * a, b * b, c * c if a + b > c and a + c > b and b + c > a: if a2 + b2 == c2 or a2 + c2 == b2 or b2 + c2 == a2: print('Right Triangle') elif a2 + b2 < c2 or a2 + c2 < b2 or b2 + c2 < a2: print('Obtuse Triangle') elif a2 + b2 > c2 and a2 + c2 > b2 and b2 + c2 > a2: print('Acute Triangle') else: print('Not Triangle') ``` ## [C_MM40-易] 1~N之間的總和 ```python= while True: N = int(input()) s = 1 print(1, end='') for i in range(2, N + 1): s += i print(' + {}'.format(i), end='') print(' = {}'.format(s)) ``` ## [C_MM42-中] 求(-1)^(n+1)x[1/(2n-1)]的和 ```python= s = [0] * 16 s[1] = 1.0 for i in range(2, 16): s[i] = s[i - 1] + ((-1) ** (i + 1)) / (2 * i - 1) while True: n = int(input()) print('{0:.3f}'.format(s[n])) ``` ## [C_MM44-易] The Numbers ```python= while True: N, M = input().split() cnt = 0 for i in range(0, 6): if M[i] == N[0] and M[i + 1] == N[1]: cnt += 1 print(cnt) ``` ## [C_MM45-中] 分禮物 ```python= class Participant: def __init__(self, name, gift_name): self.name = name self.gift_name = gift_name class Permutation: step = 0 n = 0 cnt = 0 def __init__(self, n): self.n = n self.list_name = [] self.arrangement = [''] * n self.book = [False] * n self.result = '' def dfs(self, step): if step == n: for k in range(n): if self.list_name[k].gift_name == self.arrangement[k]: return for k in range(0, n): if k != 0: self.result += ',' self.result += '{} {}'.format(self.list_name[k].name, self.arrangement[k]) self.result += '\n' self.cnt += 1 for j in range(n): if self.book[j]: continue self.arrangement[step] = self.list_name[j].gift_name self.book[j] = True self.dfs(step + 1) self.book[j] = False def answer(self): print(self.cnt) print(self.result, end='') n = int(input()) p = Permutation(n) for i in range(0, n): name, gift_name = input().split() p.list_name.append(Participant(name, gift_name)) p.dfs(0) p.answer() ``` ## [C_MM46-易] 複數運算 ```python= n = int(input()) for i in range(0, n): opr, a, b, c, d = input().split() A = complex(int(a), int(b)) B = complex(int(c), int(d)) if opr == '+': C = A + B elif opr == '-': C = A - B elif opr == '*': C = A * B print(int(C.real), int(C.imag)) ``` ## [C_MM48-易] F91 ```python= def f91(z): if z >= 101: return z - 10 else: return 91 k = int(input()) n = list(map(int, input().split())) for x in n: print(f91(x)) ``` ## [C_MM49-易] 連續1的倍數 ```python= def f(x): num = '1' while int(num) % x != 0: num += '1' else: return len(num) k = int(input()) n = list(map(int, input().split())) for i in range(k): print(f(n[i])) ``` ## [C_MM50-易] 分贓 ```python= while True: k = int(input()) things = list(map(int, input().split())) total = sum(things) half = total // 2 bag = [0] * (half + 1) for w in things: for j in range(half, w - 1, -1): bag[j] = max(bag[j - w] + w, bag[j]) print(abs(total - 2 * bag[half])) ``` --- > [time=Sun, Jun 6, 2021 7:00 PM]