1. 程式人生 > >【Python】python基礎練習題_1

【Python】python基礎練習題_1

1. 100個例子

expamle1: 有四個數字:1、2、3、4,能組成多少個互不相同且無重複數字的三位數?各是多少?

我的程式碼:

num={1,2,3,4}
for i in num:
    bnum=list(num)
    bnum.remove(i)
    for b in bnum:
        cnum=list(bnum)
        cnum.remove(b)
        for c in cnum:
            print i,b,c

官方程式碼:

for i in range(1,5):
    for j in
range(1,5): for k in range(1,5): if( i != k ) and (i != j) and (j != k): print i,j,k

總結:

可以使用range與判斷

 

expamle2:企業發放的獎金根據利潤提成。利潤(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,求應發放獎金總數?

我的程式碼:

I=raw_input("please input i:")
I=int(I)
print I
bonus=0
if(I<=100000):
    bonus=I*0.1
elif(I<200000):
    bonus=100000*0.1+(I-100000)*0.075
elif(I<400000):
    bonus = 100000 * 0.1 + 100000 * 0.075+200000*0.05
elif(I<600000):
    bonus = 100000 * 0.1 + 100000 * 0.075 + 200000 * 0.05+200000*0.03
elif (I < 1000000):
    bonus = 100000 * 0.1 + 100000 * 0.075 + 200000 * 0.05 + 200000 * 0.03+400000*0.015
else
: bonus = 100000 * 0.1 + 100000 * 0.075 + 200000 * 0.05 + 200000 * 0.03+400000*0.015+(I-1000000)*0.01 print bonus

官方程式碼

i = int(raw_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

 

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

MonthArray=[31,28,31,30,31,30,31,31,30,31,30,31]

date=raw_input("please input the date:")
datearray=date.split("-")
year=datearray[0]
month=datearray[1]
day=datearray[2]
result=0

if (int(month)>1):
    for i in range(int(month)-1):
        result=result+MonthArray[i]
    result=result+int(day)
    if(int(year)%4==0):
        result=result+1
else:
    result=day

print result

官方程式碼:

year = int(raw_input('year:\n'))
month = int(raw_input('month:\n'))
day = int(raw_input('day:\n'))
 
months = (0,31,59,90,120,151,181,212,243,273,304,334)
if 0 < month <= 12:
    sum = months[month - 1]
else:
    print 'data error'
sum += day
leap = 0
if (year % 400 == 0) or ((year % 4 == 0) and (year % 100 != 0)):
    leap = 1
if (leap == 1) and (month > 2):
    sum += 1
print 'it is the %dth day.' % sum

 

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

我的程式碼:

x=int(raw_input("x="))
y=int(raw_input("y="))
z=int(raw_input("z="))

array=[x,y,z]
test=sorted(array)
print test

官方程式碼:

l = []
for i in range(3):
    x = int(raw_input('integer:\n'))
    l.append(x)
l.sort()
print l