T = int(input()) for _ inrange(T): a, b = map(int, input().split()) total = 0 if a % 2 == 0: a += 1 for i inrange(a, b+1, 2): total += i print(total)
就這樣,應該還有更簡潔的寫法,不過能過就是好寫法。
E
1 2 3
n = int(input()) rev = int(str(n)[::-1]) print(rev)
沒啥好說。
F
1 2 3 4 5 6 7 8 9 10 11 12
s = input() target = "hello" j = 0 for c in s: if c == target[j]: j += 1 if j == len(target): break if j == len(target): print("YES") else: print("NO")
就這樣而已,沒有必要想得太難哈。
G
1 2 3 4 5 6 7
s = input().lower() vowels = set("aoyeui") result = "" for c in s: if c notin vowels: result += "." + c print(result)
就這樣,寫的時候要注意看題目,我以為只要看aeiou就好了,繳完結果還有一個y,哭。
H
1 2 3 4 5 6 7 8 9 10
a, b = input().split()
if a == b: print(0) elif (a == "B"and b == "T") or (a == "T"and b == "C") or (a == "C"and b == "W") or (a == "W"and b == "B"): print(1) elif (b == "B"and a == "T") or (b == "T"and a == "C") or (b == "C"and a == "W") or (b == "W"and a == "B"): print(2) else: print(0)
就這樣慢慢寫,要用字典也行啦。
I
1 2 3 4 5 6
s = input() password = "" for i inrange(len(s)-1): dist = abs(ord(s[i]) - ord(s[i+1])) password += str(dist) print(password)
嗯,好像其實也還好,基本題而已。
J
1 2 3 4 5 6
ip = input().split(".") binary_ip = "" for part in ip: binary_part = bin(int(part))[2:].zfill(8) binary_ip += binary_part print(binary_ip)
for _ inrange(T): N = int(input()) arr = list(map(int, input().split())) max_ending_here = 0 max_so_far = -1000000 for num in arr: max_ending_here += num if max_ending_here > max_so_far: max_so_far = max_ending_here if max_ending_here < 0: max_ending_here = 0 if max_so_far > 0: print(f"The maximum winning streak is {max_so_far}.") else: print("Losing streak.")
for _ inrange(T): a,b = list(map(int,input().split())) count = 0 f1 = 1 f2 = 2 if f1 >= a and f1 <= b: count += 1 if f2 >= a and f2 <= b: count += 1 whileTrue: f3 = f1 + f2 if f3 > b: break if f3 >= a: count += 1 f1, f2 = f2, f3 print(count)
Fibonacci建議用dp,要用遞迴可以用@cache。
N
1 2 3 4 5 6 7 8 9 10 11
n = int(input()) points = []
for _ inrange(n): x, y = map(int, input().split()) points.append((x, y))
is_prime_small = [True] * (MAX + 1) is_prime_small[0] = is_prime_small[1] = False for i inrange(2, int(math.isqrt(MAX)) + 1): if is_prime_small[i]: for j inrange(i*i, MAX+1, i): is_prime_small[j] = False defis_prime(n): if n <= MAX: return is_prime_small[n] for i inrange(2, MAX+1): if is_prime_small[i] and n % i == 0: returnFalse returnTrue
T = int(input()) for _ inrange(T): N = int(input()) ifnot is_prime(N): print(f"{N} is not a prime number.") else: R = int(str(N)[::-1]) if R != N and is_prime(R): print(f"{N} is an emirp number.") else: print(f"{N} is a prime number.")
記得要學埃式篩喔。
P
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
T = int(input())
for _ inrange(T): N = int(input()) n = N sum_factors = 0 i = 2 while i*i <= n: while n % i == 0: sum_factors += i n //= i i += 1 if n > 1: sum_factors += n print(sum_factors)
T = int(input()) for _ inrange(T): m, n = map(int, input().split()) edges = [] total_sum = 0 for _ inrange(n): x, y, z = map(int, input().split()) edges.append((z, x, y)) total_sum += z
edges.sort() parent = list(range(m)) mst_sum = 0 for z, x, y in edges: if union(parent, x, y): mst_sum += z