《算法競賽入門經典》——第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')

 

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