print('用2,4,8,9 `组成不重复的3位数:') l=0 for i in (2,4,8,9): for j in (2,4,8,9): for k in (2,4,8,9): if i!=j and i!=k and j!=k: print(i*100+j*10+k) l+=1 print('共有%d个不同的3位数。'%l)
deftest_02_1(): 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 inrange(0, 6): if i > arr[idx]: # print(idx) r += (i - arr[idx]) * rat[idx] # print((i - arr[idx]) * rat[idx]) i = arr[idx] print(r) return r
3. 判断1000以内的完全平方数
问题:一个整数,它加上100后是一个完全平方数,再加上268又是一个完全平方数, 请问该数是多少?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
import math
#笨办法1,效率低下: defcheck_sqrt1(): print('用多个循环嵌套来解决') num1=1 num2=False while num1<1000: for i inrange(num2,num1): for j inrange(num2,num1): for k inrange(num2,num1): if j**2==i+100and k**2==i+268: print('这个数是%d'%i) num2=True if num2: break num1+=1
1 2 3 4 5 6 7 8 9 10 11 12
defcheck_sqrt2(): #用数学函数平方根sqrt()来解决,效率高 print('用int(sqrt())==sqrt() 来判断') num=1 while num <1000: if math.sqrt(num + 100)==int(math.sqrt(num+100)) and math.sqrt(num+268)==int(math.sqrt(num+268)): print('这个数是:%d '%num) num+=1
#check_sqrt1() check_sqrt2()d '%num) num+=1
4. 判断一年中的第几天的问题
问题描述:输入某年某月某日,判断这一天是这一年的第几天?
思路:要判断这年有多少天,是不是闰年,再算是第几天
闰年:被4整除、不被100整除,整百年被400整除
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
defday_in_year(date): month_days=[31,28,31,30,31,30,31,31,30,31,30,31] year=int(date[:4]) month=int(date[4:6]) day=int(date[6:]) print(year,month,day) if year % 4==0and year%100!=0or year % 400==0: month_days[1]=29 dayth=0 for i inrange(month-1): dayth+=month_days[i] dayth+=day return dayth
for i in range(100,1000): bai=int(i/100) shi=int(((i-int(i/100)*100))/10) ge=int((i-int(i/10)*10)) if i==bai**3+shi**3+ge**3: print('%d = %d + %d + %d '%(i,bai**3,shi**3,ge**3))
def zys(num): for i in range(2,num): if num%i==0: yinshu.append(str(i)) if num/i >i: zys(num//i) elif num/i>1: yinshu.append(str(num//i)) break else: yinshu.append(str(num))
num=int(input('请输入一个数字:')) if num <=1: num = int(input('输入错误,请重新输入:')) yinshu=[] zys(num) y='+'.join(yinshu) print(num,'=',y)
11. 汉诺塔问题
1 2 3 4 5 6 7 8 9 10 11 12 13 14
def hano(num,a,b,c): if num ==1: print(a,' -> ',c) return else: hano(num-1,a,c,b) print(a,' -> ',c) hano(num-1,b,a,c) return
def wanshu(start,end): for i in range(start,end): num_list=[1] for j in range(2,i): if i%j==0: num_list.append(j) num_list.append(i//j) if len(num_list)>2: s=0 num_list=list(set(num_list)) num_list.sort() for k in num_list: s+=k if s==i: print(num_list) print(sum(num_list)) wanshu(1,10000)
h=100.0 s=0.0 for i in range(1,11): s+=h #每次下降的距离 h=h/2 #弹起的距离 print('第%d次弹起%f米,球已行动了%f米。'\ %(i,h,s)) if i <10: s+=h #这h是前九次下落的距离,第十次不下落,总距离不再加 print(h,' ',s)
方法2:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
tour = [] height = [] hei = 100.0 # 起始高度 tim = 10 # 次数 for i in range(1, tim + 1): # 从第二次开始,落地时的距离应该是反弹高度乘以2(弹到最高点再落下) if i == 1: tour.append(hei) else: tour.append(2 * hei) hei /= 2 height.append(hei) print('总高度:tour = {0}'.format(sum(tour))) print('第10次反弹高度:height = {0}'.format(height[-1]))
jie2=1 for i in range(1,n+1): jie2*=i print('for循环求解:\n%d 的阶乘是:%d \n'%(n,jie2))
15.3 while 循环求解
1 2 3 4 5
jie3=1 while i>=1: jie3 *=i i-=1 print('while循环求解:\n%d 的阶乘是:%d '%(n,jie3))
16. 输出101-200(区间数可自定)以内的素数
判断101-200之间有多少个素数,并输出所有素数。
1 2 3 4 5 6 7 8 9 10 11
count=0 for i in range(101,1001): for j in range(2,int(i/2)): if i%j==0 : #int(i/j)==i/j: print('%d 不是素数:%d=%d x %d'%(i,i,j,i/j)) break else: count+=1 print(i ,' 是素数') print('101-200之间共有%d个素数'%count)
We have two monkeys, a and b, and the parameters a_smile and b_smile indicate if each is smiling. We are in trouble if they are both smiling or if neither of them is smiling. Return True if we are in trouble.