1.随机数。
生成一个由N各元素的由随机数n组成的列表,其中N和n的去值范围分别为(1<=N<=100),(0<=n<=2^31-1)。然后再随机从这个列表中取N(1<=N<=100)个随机数出来,对它们进行排序,然后显示整个子集
def suijishu():
import random
num=random.randint(1,100)
lst=[]
for item in range(num):
tmp=random.randint(0,(pow(2,31)-1))
lst.append(tmp)
lst.sort()
print lst
suijishu()
2.最大公约数和最小公倍数
def fun7():
a=input('please input a number:')
b=input('please input another number:')
su=[]
if a>b:
smaller=b
else:
smaller=a
for i in range(1,smaller+1):
if a%i==0 and b%i==0:
su.append(i)
l=len(su)
p=su[l-1]
print '最大公约数:',p
print '最小公倍数:',a*b/p
while True:
fun7()
3.判断是否是闰年
判断是否为闰年条件:
1)能够被4整除
2)能够被100整除且可以被400整除
def fun3():
year=input('please input a year to test if the year is leap year:')
if year%4==0:
answer='leap year'
elif year%100==0 and year%400==0:
answer='leap year'
else:
answer='common year'
return ('%d is %s' % (year, answer))
4.取余
取一个任意小于1美元的金额,计算可以换算为最少多少枚硬币。硬币有1美分,5美分,10美分,25美分,1美元可以兑换100美分
def fun4():
money=input('please a number of money you want to have some change:')
if money not in range(100):
return ('please input the number which is in the range about 0~100')
else:
change25=money/25
change10=money%25/10
change5=money%25%10/5
change1=money%25%10%5
return('%d can have the less change are %d 25$, %d 10$, %d 5$, %d 1$'
% (money, change25,change10, change5, change1))
5.几何
计算面积和体积
1)正方形和立方体
2)圆和球体
def fun6():
print '''
1.area
2.volume'''
chioce1=input('please input the chioce:')
if chioce1==1:
print '''
1.square
2.circle'''
chioce2=input('please input the chioce you want to compute:')
if chioce2==1:
a=input('please input the lenth:')
b=input(('please input the width:'))
return (a*b)
elif chioce2==2:
r=input('please input the radii:')
return (3.14*(float(r)**2))
else:
return ('please input the chioce like (1|2)')
elif chioce1==2:
print '''
1.cube
2.ball'''
chioce2 = input('please input the chioce you want to compute:')
if chioce2 == 1:
a = input('please input the lenth:')
b = input('please input the width:')
c = input('please input the highth:')
return (a * b * c)
elif chioce2 == 2:
r = input('please input the radii:')
return (3.14 * (float(r) ** 2 * 4/3))
else:
return ('please input the chioce like (1|2)')
else:
return ('please the input the chioce like (1|2)')
6.算术
输入两个数和运算符,可以支持的运算为+,-,,/,%,*
def fun5():
numbers=raw_input('please input two numbers and operation like this (num1,num2,oper): ')
num1=float(numbers.split(',')[0])
num2=float(numbers.split(',')[1])
oper=numbers.split(',')[2]
if oper=='+':
return (num1+num2)
elif oper=='-':
return (num1-num2)
elif oper=='*':
return (num1*num2)
elif oper=='/':
return (num1/num2)
elif oper=='%':
return (num1%num2)
elif oper=='**':
return (num1**num2)
else:
return ('please input the oper like this (+|-|*|/|%|**)')