學習python,每日練習0528

題目
企業發放的獎金根據利潤提成。利潤(I)低於或等於10萬元時,獎金可提10%;
利潤高於10萬元,低於20萬元時,低於10萬元的部分按10%提成,高於10萬元的部分,可提成7.5%;
20萬到40萬之間時,高於20萬元的部分,可提成5%;40萬到60萬之間時高於40萬元的部分,可提成3%;
60萬到100萬之間時,高於60萬元的部分,可提成1.5%,高於100萬元時,超過100萬元的部分按1%提成,從鍵盤輸入當月利潤I,求應發放獎金總數?
自己寫的:比較好理解,但是代碼數量多


#定義變量profit:用於接收鍵盤輸入的利潤值(注意鍵盤輸入的是字符串類型,需要轉換成整型)
#定義變量bonus來記錄獎金總數
profit = int(input("請輸入當月利潤:"))
if profit <= 100000:
    bonus = profit*0.1
    print("當月利潤爲%d元,應發獎金總數%d元"%(profit,bonus))
elif profit >100000 and profit<=200000:
    bonus = 100000*0.1+(profit-100000)*0.075
    print("當月利潤爲%d元,應發獎金總數%d元" % (profit, bonus))
elif profit > 200000 and profit <=400000:
    bonus = 100000*0.1+100000*0.075+(profit-200000)*0.05
    print("當月利潤爲%d元,應發獎金總數%d元" % (profit, bonus))
elif profit > 400000 and profit <= 600000:
    bonus = 100000*0.1+100000*0.075+200000*0.05 +(profit-400000)*0.03
    print("當月利潤爲%d元,應發獎金總數%d元" % (profit, bonus))
elif profit > 600000 and profit <= 1000000:
    bonus = 100000*0.1+100000*0.075+200000*0.05 +200000*0.03+(profit-600000)*0.015
    print("當月利潤爲%d元,應發獎金總數%d元" % (profit, bonus))
elif profit >1000000:
    bonus = 100000*0.1+100000*0.075+200000*0.05 +200000*0.03+400000*0.015+(profit-1000000)*0.01
    print("當月利潤爲%d元,應發獎金總數%d元" % (profit, bonus))

其他寫的:邏輯較深,不好理解,但是代碼數量少,耦合性低很多

i = int(input('淨利潤:'))
arr = [1000000,600000,400000,200000,100000,0]
rat = [0.01,0.015,0.03,0.05,0.075,0.1]
r = 0
for idx in range(0,6):
    if i > arr[idx]:
        r += (i - arr[idx]) * rat[idx]
        print((i-arr[idx]) * rat[idx])
        i = arr[idx]
print(r)

一個整數,它加上100和加上268後都是一個完全平方數,請問該數是多少?
解題思路:設置這個數爲i,那麼i+100 = xx;那麼(i+100)+168 = yy;因此需要滿足:x*x+168 是完全平方根


print([x*x-100 for x in range(100) if (x*x+168)**0.5 % 1 == 0])

輸入某年某月某日,判斷這一天是這一年的第幾天?
閏年的定義:四年一閏,百年不閏,四百年再閏
如果是閏年,則2月份要增加1天

#定義變量year:接受輸入的年份
year = int(input("請輸入年份:"))
#定義變量month:接收輸入的月份
month = int(input("請輸入月份:"))
#定義天數的list
listDay = [31,28,31,30,31,30,31,31,30,31,30,31]
#定義變量day:接收輸入的日期
day = int(input("請輸入日期:"))
#定義theDay來存放是這一年的第幾天
theDay = 0
if  month == 2:
    theDay = 31 + day
    print("%d年%d月%d日是這一年的第%d天"%(year,month,day,theDay))
elif month == 1:
    theDay = day
    print("%d年%d月%d日是這一年的第%d天"%(year,month,day,theDay))
elif month>2:
    if year%4 == 0 and year%100 != 0 or year%400 == 0:
        for i in range(1,month):
            theDay += listDay[i-1]
        print("%d年%d月%d日是這一年的第%d天"%(year,month,day,theDay+1+day))
    else:
        for i in range(1,month):
            theDay += listDay[i-1]
        print("%d年%d月%d日是這一年的第%d天" % (year, month, day, theDay + day))

請輸入年份:2000
請輸入月份:12
請輸入日期:31
2000年12月31日是這一年的第366天

‘’’
輸入三個整數x,y,z,請把這三個數由小到大輸出
‘’’

#分別定義i,j,k三個變量來接收三個整數
i = int(input("請輸入第一個整數:"))
j = int(input("請輸入第二個整數:"))
k = int(input("請輸入第三個整數:"))
#定義list來存放這三個數字
list = [i,j,k]
list.sort()
print(list)

題目來自:
https://fishc.com.cn/forum.php?mod=collection&action=view&ctid=588

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