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: ')) month = int(raw_input('month: ')) day = int(raw_input('day: ')) 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: ')) l.append(x) l.sort() print l