a, b, c = map(int, input().split()) condition = a % 2 == 1and b % 2 == 1and c % 2 == 1 print(int(condition))
章节3:关系、逻辑运算与选择语句
1012 判断奇偶数
1 2 3 4 5 6
x = int(input())
if x % 2 == 0: print('是') else: print('不是')
1014 两数比大小
1 2 3 4 5 6
a, b = map(int, input().split())
if a > b: print(a) else: print(b)
1018 恐龙园买门票
1 2 3 4 5 6
h = float(input())
if h < 1.3: print(60) else: print(120)
1130 心系南方灾区
1 2 3
from math import ceil a,b = map(int, input().split()) print(ceil(a/b))
1013 判断闰年
1 2 3 4 5 6
a = int(input())
if a % 400 == 0or (a % 4 == 0and a % 100 != 0): print('RUN!') else: print('NO RUN!')
267 左邻右舍
1 2 3 4 5 6 7 8
n, x = map(int, input().split())
a, b = x - 1, x + 1 if a == 0: a = n if b == n + 1: b = 1 print(min(a, b), max(a, b))
1366 出租车计费
1 2 3 4 5 6 7 8
w = float(input())
if w <= 3: print('8.0') elif w <= 10: print('%.1f'%(8+(w-3)*1.8)) else: print('%.1f'%(8+7*1.8+(w-10)*2.4))
1010 三个数的大小关系
1 2 3 4 5 6
a, b, c = map(int, input().split())
if a < b < c: print('Yes') else: print('No')
1370 BMI指数
1 2 3 4 5 6 7 8 9 10 11
a, b = map(float, input().split())
x = b / (a * a) if x <= 18.4: print('偏瘦') elif x <= 23.9: print('正常') elif x <= 27.9: print('过重') else: print('肥胖')
272 加油卡充值
1 2 3 4 5 6 7 8 9 10
n = int(input())
if n >= 5000: print('%.2f'%(n+n*0.05)) elif n >= 3000: print('%.2f'%(n+n*0.04)) elif n >= 1000: print('%.2f'%(n+n*0.03)) else: print('%.2f'%n)
266 **解方程
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
a, b, c = map(int, input().split())
if a == 0: if b == 0: if c == 0: print('任意实数') else: print('无解') else: print('%.1f'%(-c / b)) else: delta = b * b - 4 * a * c if delta > 0: print('%.1f'%((-b-delta**0.5)/2/a), end=' ') print('%.1f'%((-b+delta**0.5)/2/a)) elif delta == 0: print('%.1f'%(-b/2/a)) else: print('无解')
n = int(input()) ans = 0.0 for i inrange(1, n + 1): ans += 1 / i print('%.3f'%ans)
1025 求1*1+2*2+…+n*n
1 2 3 4 5
n = int(input()) ans = 0 for i inrange(1, n + 1): ans += i ** 2 print(ans)
1124 分别求奇数和偶数和
1 2 3 4 5 6 7 8 9 10
n = int(input()) ans1, ans2 = 0, 0 for i inrange(n): t = int(input()) if t % 2 == 0: ans2 += t else: ans1 += t; print(ans1) print(ans2)
1382 求约数和
1 2 3 4 5 6
n = int(input()) ans = 0 for i inrange(1, n + 1): if n % i == 0: ans += i print(ans)
1026 求n!
1 2 3 4 5
n = int(input()) ans = 1 for i inrange(1, n + 1): ans *= i print(ans)
1131 **判奇偶求和
1 2 3 4 5 6 7 8 9 10 11
n = int(input()) ans = 0 if n % 2 == 1: for i inrange(1, n + 1): if i % 2 == 0: ans += i else: for i inrange(1, n + 1): if n % i == 0: ans += i print(ans)
1027 **求1!+2!+…+n!
1 2 3 4 5 6 7 8 9 10 11
deffactorial(n: int): ans = 1; for i inrange(1, n + 1): ans *= i return ans
n = int(input()) ans = 0 for i inrange(1, n + 1): ans += factorial(i) print(ans)
for i inrange(1, n + 1): for j inrange(i + 1, n + 1): if cond(i, j): ans += 1
print(ans)
章节8:算法基础
1363 递归法求斐波拉契数列第n项的值
1 2 3 4 5 6
deff(n): if n == 1or n == 2: return1 return f(n - 1) + f(n - 2) n = int(input()) print(f(n))
1447 递归求n!
1 2 3
import math n = int(input()) print(math.factorial(n))
1448 递归法实现辗转相除算法
1 2 3
import math a, b = map(int, input().split()) print(math.gcd(a, b))
1028 枚举法求水仙花数
1 2 3 4 5 6 7 8
m, n = map(int, input().split()) f = True for i inrange(m, n + 1): if i == sum(map(lambda x: (ord(x) - ord('0')) ** 3, str(i))): print(i, end=' ') f = False if f: print('no')
1031 枚举法求完美数
1 2 3 4 5 6 7 8 9 10 11 12 13 14
defget_divisors_sum(n): s = 0 for i inrange(1, int(n ** 0.5) + 1): if n % i == 0: s += i if i * i != n and i != 1: s += n // i return s
m, n = map(int, input().split())
for i inrange(m, n + 1): if i == get_divisors_sum(i): print(i, end=' ')
1362 递推法求斐波拉契数列第n项的值
1 2 3 4 5 6 7 8
n = int(input()) a, b = 1, 1 if n <= 2: print(1) exit(0) for i inrange(n - 2): a, b = b, a + b print(b)