一篷角、萬年歷
它的功能是:用戶輸入某年、某月恳蹲、某日虐块,程序輸出這一天是星期幾;用戶輸入某年嘉蕾、某月贺奠,程序輸出該年該月的每一天是星期幾荆针;用戶輸入某年敞嗡,程序輸出該年每一月的每一天是星期幾。
該程序的求解方案是喉悴,為給定的年、月玖媚、日計算它距離1900年1月1日(它是星期一)的天數(shù)箕肃,從而得出是星期幾;注意要考慮閏年的情況今魔。
def leap_year(year):
if year%4 == 0 and year%100 != 0 or year%400 == 0:
return True
else:
return False
def days_of_month(year,month):
days = 31
if month == 2:
if leap_year(year):
days = 29
else:
days = 28
elif month == 4 or month == 6 or month == 9 or month == 11:
days = 30
return days
def totaldays(year,month,day):
totaldays = 0
for i in range(1900,year):
if leap_year(i):
totaldays += 366
else:
totaldays += 365
for j in range(1,month):
totaldays += days_of_month(year,j)
totaldays = totaldays+day-1
return totaldays
def print_month(year,month):
print ("一 二 三 四 五 六 日")
print (totaldays(year,month,1)%7*' ',end="")
for i in range(1,days_of_month(year,month)+1):
if i < 10:
print(i,3*" ",end="")
else:
print(i,2*" ",end="")
if (totaldays(year,month,i)+1)%7 == 0:
print(" ")
choice = int(input("年份日歷請選1,月份日歷請選2错森,某天周幾請選3:"))
if choice == 1:
year = int(input("請輸入年份:"))
for month in range(1,13):
print("2018年"+str(month)+"月")
print(print_month(year,month))
if choice == 2:
year = int(input("清輸入年份:"))
month = int(input("請輸入月份:"))
print(print_month(year,month))
elif choice == 3:
year = int(input("清輸入年份:"))
month = int(input("請輸入月份:"))
day = int(input("請輸入一月中的第幾天:"))
weekday = totaldays(year,month,day)%7+1
print("這一天是星期"+str(weekday))
else:
print("請輸入1到3的數(shù)字吟宦!")
二、泰勒法求e值
問題: e = 1 + 1/1! + 1/2! + 1/3! + ... (≈??.??????????…) 基于上式求e(自然常數(shù))的近似值涩维,要求最后一項都大于等于10-4
def abc(n):
if n == 1:
return 1
return n*abc(n-1)
for n in range(1,10000):
if 1/abc(n) < 0.1**4:
x = n
break
e = 1
for n in range(1,x+1):
e += 1/abc(n)
print(e)