《算法竞赛入门经典》——第1章课后习题 (Python版本)

习题1-1 平均数

from decimal import *

a = input()
b = input()
c = input()

a = int(a)
b = int(b)
c = int(c)

getcontext().prec = 3
print(Decimal(a+b+c) / Decimal(3))

习题1-2 温度

from decimal import *

f = input()
f = int(f)
getcontext().prec = 5

print(Decimal(5*(f-32))/Decimal(9))

习题1-3 连续和

n = input()
n = int(n)

sum = 0
for i in range(1, n+1):
    sum += i

print(sum)

习题1-4 正弦和余弦

import math

n = int(input())

print(math.sin(n))
print(math.cos(n))

习题1-5 打折

count = int(input())

if count * 95 < 300:
    print('%.2f' % float(count * 95))
elif count * 95 >= 300:
    print('%.2f' % float(count * 95 * 0.85))
    

习题1-6 三角形

a = int(input())
b = int(input())
c = int(input())

if a + b > c and a + c > b and b + c > a:
    print('yes')
else:
    print('no')

习题1-7 年份

year = int(input())

if year % 400 == 0 or (year % 4 == 0 and year % 100 != 0):
    print('yes')
else:
    print('no')

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章