• 當前位置:首頁 > IT技術 > 編程語言 > 正文

    python練習冊(三)
    2022-05-31 17:19:13

    Hi,小編本周又來送練習題了,編程肯定要多多練習啦!

    題目:

    輸入某年某月某日,判斷這一天是這一年的第幾天?

    1.程序分析:以3月5日為例,應該先把前兩個月的加起來,
    然后再加上5天即本年的第幾天,特殊情況,
    閏年且輸入月份大于3時需考慮多加一天。

    """
    輸入某年某月某日,判斷這一天是這一年的第幾天?
    """
    year=input("請輸入年")
    month=input("請輸入月")
    day=input("請輸入日")
    if year.isdigit() and month.isdigit() and day.isdigit() and len(year)==4 and 0<len(month)<=2 and 0<len(day)<=2:
        year=int(year)
        month=int(month)
        day=int(day)
    else:
        print("年月日輸入有誤,必須輸入數字")
    
    all_day=0
    if month>1:
        all_day+=31
    if month>2:
        if year%4==0 and year%100!=0 or year%400==0:
            all_day+=29
        else:
            all_day+=28
    if month>3:
        all_day+=31
    if month>4:
        all_day+=30
    if month>5:
        all_day+=31
    if month>6:
        all_day+=30
    if month>7:
        all_day+=31
    if month>8:
        all_day+=31
    if month>9:
        all_day+=30
    if month>10:
        all_day+31
    if month>11:
        all_day+=30
    
    all_day+=day
    print("{0}年{1}月{2}日是今年的第{3}天".format(year,month,day,all_day))

    ?

    題目:斐波那契數列。

    程序分析:黃金一類數列,指數列:1、1、2、3、5、8、13、21、34,可以用遞歸定義一個函數

    def fib(n):
        if n==1 or n==2:
            return 1
        return fib(n-1)+fib(n-2) #遞歸調用,自己調用自己
    
    print("---------斐波那契數列-------------")
    num=input("請輸入數字:")
    if num.isdigit():
        num=int(num)
    else:
        print("輸入的不是數字")
        
    fib_list=[]
    i=1
    while i<=num:
        fib_list.append(fib(i))
        i+=1
    print(fib_list)

    ?

    題目判斷101-200之間有多少個素數,并輸出所有素數。

    程序分析:判斷素數的方法:能被1和它自己整除的數是素數,如果能被其他數字整除,則不是素數

    rs=0
    for i in range(101,201):
        flag=False
        for j in range(2,i):
            if i%j==0:
                flag=True
    print(i)
    break if not flag: rs+=1 print("%d是素數"%i) print("素數的個數是%d"%rs)

    本文摘自 :https://www.cnblogs.com/

    開通會員,享受整站包年服務
    国产呦精品一区二区三区网站|久久www免费人咸|精品无码人妻一区二区|久99久热只有精品国产15|中文字幕亚洲无线码