2018-05-24

一、萬年歷

它的功能是:用戶輸入某年、某月、某日,程序輸出這一天是星期幾;用戶輸入某年、某月,程序輸出該年該月的每一天是星期幾;用戶輸入某年,程序輸出該年每一月的每一天是星期幾。
該程序的求解方案是,為給定的年、月、日計算它距離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)
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • mysql 優(yōu)化數(shù)據庫設計 第一范式(1st NF) 第一范式的目標是確保每列的原子性定義:數(shù)據庫表中的所有字段都...
    xyxSUN閱讀 451評論 0 1
  • 姓名 顧春陽 公司 揚州市方圓建筑有限公司 打卡第190天 【知~學習】 《六項精進》1遍 共628遍 《大學》1...
    顧春陽閱讀 205評論 0 0
  • 前面早已說過,漢朝興立以來,無論劉邦、呂雉、文帝、景帝對于匈奴,既恨又怕,年輕的劉徹繼位之后,天天都在想著能有什么...
    懶蟲小獅子閱讀 303評論 0 0
  • 1. 單詞部分 Egypt n. 埃及Egyptian n. 埃及人 adj. 埃及的 abroad adv. 國...
    張無忌_閱讀 1,198評論 0 1
  • 九月九日憶山東兄弟 [ 唐代·王維 ] 獨在異鄉(xiāng)為異客,每逢佳節(jié)倍思親。 遙知兄弟登高處,遍插茱萸少一人。 又是一...
    莫相問閱讀 632評論 1 0

友情鏈接更多精彩內容