PTA的Python练习题(十六)

第4章-15 换硬币

挺难的,这里学到一个range的用法:

也就是说range函数能实现顺序和倒序,取决于step是正是负

count = 0
x = int(input())
a = x // 5          
for m in range(a,0,-1):
    b = (x - (m * 5)) // 2     
    for n in range(b,0,-1):
        c = x - (5 * m) - (2 * n)
        if m > 0 and n > 0 and c > 0:
            print("fen5:{}, fen2:{}, fen1:{}, total:{}".format(m,n,c,m + n + c))
            count += 1
 
print("count = {}".format(count))

第4章-16 jmu-python-判断是否构成三角形

a,b,c=map(int,input().split())
if a+b>c and a+c>b:
    print('yes')
else:
    print('no')

第4章-17 水仙花数

a=int(input())
for i1 in range(1,10):
    for i2 in range(0,10):
        for i3 in range(0,10):
          if a==3:
             total=i1*100+i2*10+i3*1
             if (total==i1**3+i2**3+i3**3):
                print('{}'.format(total))

问题是这里没法算4位和5位的水仙花数

参考别人的代码:

import math
n=int(input())
for i in range(int(math.pow(10,n-1)),int(math.pow(10,n))):
    sum=0
    j=i
    while(j>=1):
        sum=sum+math.pow(j%10,n)
        j=j//10
    if(sum==i):
        print('{:d}'.format(i))

看不太懂代码是用什么来算的

猜你喜欢

转载自www.cnblogs.com/echoDetected/p/12344889.html