浙大版《Python 程序设计》题目集 第四章答案

请用crtl+f查找对应章节
如找第四章第5题则 :crtl+f 4-5
去掉注释便可运行改代码


'''T4-1
n = int(input())
for i in range(n+1):
    print("pow(3,%d) = %d"%(i,pow(3,i)))
'''


'''T4-2
n,m = input().split()
n = int(n)
m = int(m)
cnt = 0
sum = 0
if n<=2 :
    n=2
while n<=m:
    tmp = n
    lo = 2
    flag=1
    while lo*lo<=tmp:
        if tmp%lo==0:
            flag=0
            break
        lo+=1
    if flag==1:
        sum+=tmp
        cnt+=1
    n+=1
print(cnt,sum)
'''


'''T4-3
n = int(input())
sum=1
n-=1
while n :
    n-=1
    sum = (sum+1)*2
print(sum)
'''

'''T4-4
import  math
def is_p(n):
    if n<=1:
        return False
    else:
        for i in range(2,int(math.sqrt(n)+1)):
            if n%i==0:
                return  False
        return True
x = int(input())
for i in range(2,x//2+1):
    if is_p(i)==True and is_p(x-i)==True:
        print(x,"= %d + %d"%(i,x-i))
        break
'''

'''T4-5
a = 1
sum = 1
n=int(input())
for i in range(1, n+1):
    a*= i;
    item=1 / a;
    sum += item;
print("{:.8f}".format(sum))
'''



'''T4-6
def fib(n):
    a,b = 0,1
    for i in range(n+1):
        a,b=b,a+b
    return a

n = int(input())
if n >0:
    for i in range(n):
        print('{:11d}'.format(fib(i)),end="");
        if((i+1)%5==0):
            print()
else:
    print("Invalid.")
'''


'''T4-7
n = int(input())

if(n==0):
    print('average = 0.0')
    print('count = 0')
    exit(0)
lis = list(map(int,input().split()))
sum=0
cot=0
for i in range(len(lis)):
    sum+=lis[i]
    if lis[i]>=60:
        cot+=1
avg = sum*1.0/(len(lis))
print("average = {:.1f}".format(avg))
print("count = {:d}".format(cot))

'''


'''4-8
fenzi = 2
fenmu = 1
sum=0
n = int(input())
if n==0:
    print("{:.2f}".format(0))
    exit(0)
if n==1:
    print("{:.2f}".format(2))
    exit(0)
sum=2
for i in range(n-1):
    fenmu,fenzi = fenzi,fenzi+fenmu
    sum+=fenzi*1.0/fenmu
print("{:.2f}".format(sum))
'''


'''4-9
print('[1] apple')
print('[2] pear')
print('[3] orange')
print('[4] grape')
print('[0] exit')
lst = list(map(int,input().split()))
for i in range(0, 5):
    c = int(lst[i])
    if (c == 1):
        print('price = 3.00')
    elif (c == 2):
        print('price = 2.50')
    elif (c == 3):
        print('price = 4.10')
    elif (c == 4):
        print('price = 10.20')
    elif (c == 0):
        break
    else:
        print('price = 0.00')
'''

'''4-10
def gcd(a,b):
    return a if b==0  else gcd(b,a%b)
a,b = map(int,input().split())
p = gcd(a,b)
b = b*a/p
print("%d %d"%(p,b))
'''


'''4-11
n = int(input())
for i in range(n):
    x = int(input())
    flag=1
    for i in range(2,x):
        if x%i==0:
            flag=0
            break
    if flag==1:
        print("Yes")
    else:
        print("No")
'''

'''4-12
a,b=0,1
n = int(input())
while(True):
    if a>n:
        print(a)
        break
    a,b = b,a+b
'''




'''4-13
import math
e = float(input())
a=1
i=2
t=2
b=1/t
sum=2
while(a-b>=e):
    a=b
    sum=sum+a
    i=i+1
    t=t*i
    b=1/t
print('{:.6f}'.format(sum))
    '''
    
    
'''4-14
count=letter=digit=other=blank=0
text  = input()
while True:
    for i in text:
        count+=1
        if i<='9' and i>='0':
            digit+=1
        elif i<='z' and i>='a' or i>='A' and i<='Z':
            letter+=1
        elif i==' ' or i=='\n':
            blank+=1
        else:
            other+=1
    if count==10:
        break;
    text = input()
    blank+=1
    count+=1
print('letter = {:d}, blank = {:d}, digit = {:d}, other = {:d}'.format(letter,blank,digit,other))
'''

'''4-15   写法1
target = int(input())
count=0

lis_fen5=list()
lis_fen3=list()
lis_fen1=list()


for fen5 in range(1,20):
    for fen2 in range(1,50):
        for fen1 in range(1,100):
            if fen5*5+fen2*2+fen1*1==target:
                count+=1
                lis_fen5.append(fen5)
                lis_fen3.append(fen2)
                lis_fen1.append(fen1)
                #print('fen5:{:d}, fen2:{:d}, fen1:{:d}, total:{:d}'.format(fen5,fen2,fen1,fen5+fen2+fen1))
len_lis = len(lis_fen5)
len_lis-=1
while len_lis>=0:
    fen5=lis_fen5[len_lis]
    fen2=lis_fen3[len_lis]
    fen1=lis_fen1[len_lis]
    len_lis-=1
    print('fen5:{:d}, fen2:{:d}, fen1:{:d}, total:{:d}'.format(fen5,fen2,fen1,fen5+fen2+fen1))

print('count = {:d}'.format(count))
'''


'''4-16
arr = list(map(int,input().split()))
arr.sort()
a=arr[0]
b=arr[1]
c=arr[2]
if a+b>c :
    print('yes')
else :
    print('no')
    '''

'''4-17
bits = int(input())
qi=1
zhong=10
for i in range(bits-1):
    qi*=10
    zhong*=10
while qi<zhong:
    tmp=qi
    sum=0
    while tmp!=0:
        p=tmp%10
        tmp/=10
        tmp=int(tmp)
        tmp_num=1
        for i in range(bits):
            tmp_num=tmp_num*p
        sum+=tmp_num
    if sum == qi:
        print(qi)
    qi+=1
'''


'''4-18
n=int(input())
import numpy as np
vis =np.zeros([n+1,1])
count=0
num=0
flag=0
while True:
    if flag==1:
        break
    for i in range(1,n+1):
        if vis[i]==1:
            continue
        count+=1
        if count ==3:
            num+=1
            if num==n:
                print(i)
                flag=1
                break;
            else:
                count=0
                vis[i]=1
  '''
  
  
'''4-19
import numpy as np
n  = int(input())
arr = np.zeros([n,n],dtype=int);
for i in range(n):
    lis = list(map(int,input().split()))
    for j in range(n):
        arr[i][j]=lis[j]
sum=0
for i in range(n-1):
    for j in range(n-1):
        if i+j!=n-1:
            sum+=arr[i][j]
print(sum)
        
'''
'''4-20
import numpy as np
a,b=map(int,input().split())
arr = np.zeros([a,b],dtype=int)
for i in range(a):
    lis =list(map(int,input().split()))
    sum=0
    for j in range(b):
        sum+=lis[j]
    print(sum)

'''

'''4-21
import numpy as np
n = int(input())
for i in range(n):

    m = int(input())
    arr =  np.zeros([m,m])
    for j1 in range(m):
        lis = list(map(int,input().split()))
        for j in range(m):
            arr[j1][j]=lis[j]
    flag=1
    for j1 in range(m):
        for j in range(m):
            if j1>j:
                if arr[j1][j]!=0:
                    flag=0
    if flag==1:
        print('YES')
    else:
        print('NO')
'''


'''4-22
import numpy as np
n = int(input())
arr = np.zeros([n,n])
for i in range(n):
    lis = list(map(int,input().split()))
    for j in range(n):
        arr[i][j]=lis[j]
flag=0
for i in range(n):
    if flag==1:
        break;
    for j in range(n):
        maxn=minn=arr[i][j]
        for z in range(n):
            maxn = max(maxn,arr[i][z])
            minn = min(minn,arr[z][j])
        if maxn==arr[i][j] and minn == arr[i][j]:
            flag=1
            print(i,j)
            break;
if flag==0:
    print('NONE')
        '''
'''4-23
import numpy as np
n,m = map(int,input().split())
arr = np.zeros([n,m],dtype=int)
for i in range(n):
    lis = list(map(int,input().split()))
    for j in range(m):
        arr[i][j]=lis[j]
cnt=0
for i in range(1,n-1):
    for j in range(1,m-1):
        if arr[i-1][j]<arr[i][j] and arr[i][j-1]<arr[i][j] and arr[i+1][j]<arr[i][j] and arr[i][j+1]<arr[i][j]:
            print(arr[i][j],i+1,j+1)
            cnt+=1
if cnt==0:
    print('None',n,m)
'''


'''4-24
line = int(input())
for i  in range(1,line+1):
    for j in range(1,i+1):
        if j==1:
            print("{:d}*{:d}={:<4d}".format(j,i,i*j),end="")
        else:
            print("{:d}*{:d}={:<4d}".format(j,i,i*j),end="")
    print()
'''


'''4-25
qi="A"
line = int(input())
for i  in range(line):
    for j in range(line-i):
        print("%c "%qi,end="")
        qi=chr(ord(qi)+1)
    print()
'''

'''4-26
line = int(input())
sum=0
qi=1
for i in range(1,line+1):    
    qi*=i;
    if i%2==1:
        sum+=qi
    
print("n={:d},s={:d}".format(line,sum))
'''

'''4-27
import numpy as np
cun = np.zeros([3,3],dtype=int)
arr = list(map(int,input().split()))
i=0
for x in range(3):
    for y in range(3):
        cun[x][y]=arr[i]
        i+=1
for x in range(3):
    sum=0
    maxn=cun[x][0]
    for y in range(3):
        sum+=cun[x][y]
        maxn=max(maxn,cun[x][y])
        print("{:>4d}".format(cun[x][y]),end="")
    
    print("{:>4d}{:>4d}".format(maxn,sum))
'''    

'''4-28
import numpy as np
cun = np.zeros([3,3],dtype=int)
arr = list(map(int,input().split()))
i=0
for x in range(3):
    for y in range(3):
        cun[x][y]=arr[i]
        i+=1
for x in range(3):
    for y in range(3):
        print("{:>4d}".format(cun[y][x]),end="")
    print()
'''


'''4-29
lis_a = list(map(int,input().split()))
lis_b = list(map(int,input().split()))
len_a = lis_a[0]
len_b = lis_b[0]
lis_ans = []
for i in range(1,len_a+1):
    flag=1
    for j in range(1,len_b+1):
        if lis_a[i]==lis_b[j]:
            flag=0
    if flag==1:
        for z in range(len(lis_ans)):
            if lis_ans[z]==lis_a[i]:
                flag=0
        if flag==1:
            lis_ans.append(lis_a[i])
for i in range(1,len_b+1):
    flag=1
    for j in range(1,len_a+1):
        if lis_b[i]==lis_a[j]:
            flag=0
    if flag==1:
        for z in range(len(lis_ans)):
            if lis_ans[z]==lis_b[i]:
                flag=0
        if flag==1:
            lis_ans.append(lis_b[i])
for i in range(len(lis_ans)):
    if i==0:
        print(lis_ans[i],end="")
    else:
        print("",lis_ans[i],end="")
'''

'''4-30
a,b = map(int,input().split())
cnt=0
for i in range(a,b+1):
    ans = [1]
    sum=1
    if i!=2 and i!=6 and i!=28 and i!=496 and i!=8128:#打表试出来的,结果就这几个
        continue
    for z in range(2,i):
        if i%z==0:
            sum+=z
            ans.append(z)
            if sum>i:
                break
    if sum==i:
        cnt+=1
        print("{:d} =".format(i),end="")
        for zx in range(len(ans)):
            if zx != len(ans)-1:
                print(" {:d} +".format(ans[zx]),end="")
            else:
                print(" {:d}".format(ans[zx]))
if cnt==0:
    print("None")
        '''
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值