(1)有四个数字:1、2、3、4,能组成多少个互不相同且无重复数字的三位数?各是多少?
#!python2 #-*- coding:utf-8 -*- result=[] for i in xrange(1,5): for j in xrange(1,5): for k in xrange(1,5): if i != j and i != k and j != k: result.append((i,j,k)) result len(result)
(2)企业发放的奖金根据利润提成。利润(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,求应发放奖金总数?
#!python2 #-*- coding:utf-8 -*- revenue=float(raw_input("Please input your revenue?")) boundary=[1000000,600000,400000,200000,100000,0] rate=[0.01,0.015,0.03,0.05,0.075,0.1] bonus=0 for idx in xrange(6): if revenue>boundary[idx]: print "Over %d: %d" % (boundary[idx],(revenue-boundary[idx])*rate[idx]) bonus+=(revenue-boundary[idx])*rate[idx] revenue=boundary[idx] print "Total bonus is %d" % bonus
(3)一个整数,它加上100和加上268后都是一个完全平方数,请问该数是多少?(假定范围是10000以内)
#!python2 #-*- coding:utf-8 -*- import math for i in xrange(10000): x=int(math.sqrt((i+100))) y=int(math.sqrt(i+268)) if (x*x==i+100) and (y*y==i+268) : print i
(4)输入某年某月某日,判断这一天是这一年的第几天?
#!python2 #-*- coding:utf-8 -*- year=int(raw_input("please input year: ")) month=int(raw_input("please input month: ")) day=int(raw_input("please input day: ")) months=(0,31,59,90,120,151,181,212,243,273,304,334) sum=0 if 0<month<=12: sum=months[month-1] else: print "Please input a valid month!" sum+=day leap=0 if (year %400 ==0) or ((year%4==0) and (year%100!=0)): leap=1 print "%d year is a leap year!" %year if leap==1 and month >2: sum+=1 print "It is %dth day of year %d" %(sum,year)
(5)输入三个整数x,y,z,请把这三个数由小到大输出
#!python2 #-*- coding:utf-8 -*- l1=[] for i in xrange(3): x=int(raw_input("please input a int number: ")) l1.append(x) l1.sort() print l1
(6)9*9 乘法口诀表
#!python2 #-*- coding:utf-8 -*- for i in xrange(1,10): print for j in xrange(1,i+1): print " %d * %d = %d " %(i,j,i*j),
(7)暂停一秒输出
#!python2 #-*- coding:utf-8 -*- import time d1={1:"a",2:"b"} for k,v in d1.items(): print k,v time.sleep(5)
(8) 暂停一秒输出,并格式化当前时间
#!python2 #-*- coding:utf-8 -*- import time print time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(time.time())) time.sleep(10) print time.strftime("%y-%m-%d %H:%M:%S",time.localtime(time.time()))
(9)古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?
#!python2 #-*- coding:utf-8 -*- h = 0 leap = 1 from math import sqrt from sys import stdout for m in range(101,201): k = int(sqrt(m + 1)) for i in range(2,k + 1): if m % i == 0: leap = 0 break if leap == 1: print '%-4d' % m h += 1 if h % 10 == 0: print '' leap = 1 print 'The total is %d' % h
(10)打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身
#!python2 #-*- coding:utf-8 -*- for n in xrange(100,1000): i = n/100 j=n/10%10 z=n%10 if n==i**3+j**3+z**3: print n
(11)将一个正整数分解质因数
#!python2 #-*- coding:utf-8 -*- def func(n): print "{} =".format(n), if not isinstance(n,int) or n<=0: print "Please input a valid integer." exit(0) elif n==1: print "{}".format(n) while n!=1: for index in xrange(2,n+1): if n%index==0: n/=index if n==1: print index else : print "{} *".format(index), break func(100)
(12)一个排序
#!python2 #-*- coding:utf-8 -*- data_list=[] data_list.append({"s":1,"v":1.1}) data_list.append({"s":10,"v":1.9}) data_list.append({"s":3,"v":1.6}) data_list.append({"s":2,"v":1.2}) data_list.append({"s":8,"v":1.4}) data_list data_list.sort(key=lambda v:v.get("v"), reverse=False)
(13) 时间相关
#!python2 #-*- coding:utf-8 -*- import datetime #输出今日日期,格式为 dd/mm/yyyy today=datetime.datetime.today().strftime("%d/%m/%Y") print today #创建日期对象 birthday=datetime.date(2000,1,1) print birthday.strftime("%d/%m/%Y") #日期算术运算 tmp_date=birthday+datetime.timedelta(days=1) print tmp_date.strftime("%d/%m/%Y") #日期替换 replace_date=birthday.replace(year=2012) print replace_date.strftime("%d/%m/%Y")
(14)输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数
#!python2 #-*- coding:utf-8 -*- import string s=raw_input("Please input a word: ") letters=0 space=0 digit=0 others=0 for c in s: if c.isalpha(): letters+=1 elif c.isspace(): space+=1 elif c.isdigit(): digit+=1 else: others+=1 print "letters=%d, space =%d, digit=%d,others=%d" %(letters,space,digit,others)
(15)求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加),几个数相加有键盘控制
#!python2 #-*- coding:utf-8 -*- a=int(raw_input("Please input number: ")) n=int(raw_input("Please input loop times: ")) l=[] tn=0 for i in xrange(n): tn=tn+a a=a*10 l.append(tn) sum_l=reduce(lambda x,y:x+y,l) print l print sum_l
(16)一个数如果恰好等于它的因子之和,这个数就称为"完数"。例如6=1+2+3.编程找出1000以内的所有完数。
#!python2 #-*- coding:utf-8 -*- for i in xrange(2,1001): k=[] n=-1 s=i for j in xrange(1,s): if i%j==0: s-=j k.append(j) if s==0: print i print k
(17)一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在第10次落地时,共经过多少米?第10次反弹多高?
#!python2 #-*- coding:utf-8 -*- t=[] h=[] h2=100.0 for i in xrange(1,11): t.append(h2) h2/=2.0 h.append(h2) print sum(t) print h[9]
(18)猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下的一半零一个。到第10天早上想再吃时,见只剩下一个桃子了。求第一天共摘了多少
#!python2 #-*- coding:utf-8 -*- p=1 for i in xrange(9,0,-1): p=(p+1)*2 print p
(19)两个乒乓球队进行比赛,各出三人。甲队为a,b,c三人,乙队为x,y,z三人。已抽签决定比赛名单。有人向队员打听比赛的名单。a说他不和x比,c说他不和x,z比,请编程序找出三队赛手的名单。
#!python2 #-*- coding:utf-8 -*- for i in range(ord('x'),ord('z') + 1): for j in range(ord('x'),ord('z') + 1): if i != j: for k in range(ord('x'),ord('z') + 1): if (i != k) and (j != k): if (i != ord('x')) and (k != ord('x')) and (k != ord('z')): print 'order is a -- %s b -- %s c--%s' % (chr(i),chr(j),chr(k))
(20)
#!python2 #-*- coding:utf-8 -*- from sys import stdout for i in xrange(4): for j in xrange(3-i): stdout.write(" ") for k in xrange(2*i+1): stdout.write("*") print for i in xrange(3): for j in xrange(i+1): stdout.write(" ") for k in xrange(5-2*i): stdout.write("*") print
(21) 有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列的前20项之和。
#!python2 #-*- coding:utf-8 -*- #methon 1 a=1.0 b=2.0 s=0.0 for n in xrange(1,21): s+=b/a a,b=b,a+b print s a=1.0 b=2.0 s=0.0 #methon 2 l=[] for n in xrange(1,21): l.append(b/a) a,b=b,a+b s=reduce(lambda x,y:x+y,l) print s
(22)求1+2!+3!+...+20!的和
#!python2 #-*- coding:utf-8 -*- #methon 1 s=0 for n in xrange(1,21): j=1 for i in xrange(1,n+1): j*=i s+=j print s #method 2 s=0 i=1 for n in xrange(1,21): i*=n s+=i print s #method 3 l=xrange(1,21) s=0 def op(n): j=1 for i in xrange(1,n+1): j*=i return j s=sum(map(op,l)) print s
(23)利用递归方法求5!
#!python2 #-*- coding:utf-8 -*- def fn_1(n): if n==0: sum=1 else: sum=fn_1(n-1)*n return sum for i in xrange(6): print fn_1(i)
(24)利用递归函数调用方式,将所输入的5个字符,以相反顺序打印出来
#!python2 #-*- coding:utf-8 -*- def fn_1(s,l): if l==0: return print s[l-1], fn_1(s,l-1) s=raw_input("Please input a string:") l=len(s) fn_1(s,l)
(25)有5个人坐在一起,问第五个人多少岁?他说比第4个人大2岁。问第4个人岁数,他说比第3个人大2岁。问第三个人,又说比第2人大两岁。问第2个人,说比第一个人大两岁。最后问第一个人,他说是10岁。请问第五个人多大?
#!python2 #-*- coding:utf-8 -*- def fn_1(n): if n==1: c=10 print c return else: c=fn_1(n-1)+2 print c return c fn_1(5)
(26)一个不多于5位的正整数,要求:一、求它是几位数,二、逆序打印出各位数字。
#!python2 #-*- coding:utf-8 -*- n=int(raw_input("Please input a number:")) a=n/10000 b=n%10000/1000 c=n%1000/100 d=n%100/10 e=n%10 if a!=0: print "5 位数:",e,d,c,b,a elif b!=0: print "4 位数:",e,d,c,b elif c!=0: print "3 位数:",e,d,c elif d!=0: print "2 位数:",e,d else: print "1 位数:",e
(27)一个5位数,判断它是不是回文数。即12321是回文数,个位与万位相同,十位与千位相同。
#!python2 #-*- coding:utf-8 -*- #method 1 n=int(raw_input("Please input a number:")) a=n/10000 b=n%10000/1000 d=n%100/10 e=n%10 if a==e and b==d: print "%d 是个回文数" %(n) else: print "%d 不是个回文数"%(n) #method 2 n=int(raw_input("Please input a number:")) x=str(n) flag=True for i in xrange(len(x)/2): if x[i]!=x[-i-1]: flag=False break if flag: print "%d 是个回文数" %(n) else: print "%d 不是个回文数"%(n)
(28)请输入星期几的第一个字母来判断一下是星期几,如果第一个字母一样,则继续判断第二个字母
#!/usr/bin/python # -*- coding: UTF-8 -*- first_letter = raw_input("please input first letter(input Y abort):") if first_letter.upper() == 'S': print ('please input second letter:') second_letter = raw_input("please input:") if second_letter.upper() == 'A': print ('Saturday') elif second_letter.upper() == 'U': print ('Sunday') else: print ('data error') elif first_letter.upper() == 'F': print ('Friday') elif first_letter.upper() == 'M': print ('Monday') elif first_letter.upper() == 'T': print ('please input second letter') second_letter = raw_input("please input:") if second_letter.upper() == 'U': print ('Tuesday') elif second_letter.upper() == 'H': print ('Thursday') else: print ('data error') elif first_letter.upper() == 'W': print ('Wednesday') else: print ('data error')