Educoder:python实验六 多重循环
输出数字之内的完全数x=eval(input())#代码开始for i in range(2,x+1):sum = 0for j in range(1,i):#分解因子,求i的因子和if i%j == 0:sum +=jif i == sum:print(i)#代码结束输出阶乘等式x=eval(input())#代码开始for i in range(1,x+1):sum = 1..
·
-
输出数字之内的完全数
x=eval(input()) #代码开始 for i in range(2,x+1): sum = 0 for j in range(1,i):#分解因子,求i的因子和 if i%j == 0: sum +=j if i == sum: print(i) #代码结束
-
输出阶乘等式
x=eval(input()) #代码开始 for i in range(1,x+1): sum = 1 k = "" for j in range(1,i+1): sum *=j if j == 1: k = k+str(j) else: k = k+'*'+str(j) print("{}!={}={}".format(i,k,sum)) #代码结束
-
输出符合要求的数字
x=eval(input()) #代码开始 list = [0,1,2,3,4,5,6,7,8,9] a = list[0:(x+1)] count = 0 for i in range(1,x+1): for j in range(0,x+1): if a[j] == a[i]: continue else: s = str(a[i])+str(a[j]) for k in range(0,x+1): if a[k] == a[i] or a[k] == a[j]: continue else: m = s+str(a[k])+' ' count += 1 print(m,end='' if count%10 != 0 else "\n" ) #代码结束
-
鸡兔同笼问题
t=eval(input()) j=eval(input()) #代码开始 # x:兔子,y:鸡 if (j-2*t)%2 == 0: x = (j-2*t)*0.5 y = t-x print("鸡{}只兔子{}只".format(int(y),int(x))) else: print("无解") #代码结束
-
剩余最短木料
s=eval(input()) #代码开始 if s<42: print("木料太短,请重新输入") else: x1 = (s-42)//23 #取商 x2 = (s-42)%23 #取余 if x2+x1*4<19: n1 = 1 n2 = x1+1 zx = x2 elif x2>=19: n1 = 2 n2 = x1+1 zx = x2-19 else: for i in range(1,x1+1): if x2+4*i>=19: n1 = i+2 n2 = x1-i+1 zx = (x2+4*i)%19 break #代码结束 print("19米{}段,23米{}段,剩余{}米".format(n1,n2,zx))
-
打印图形
zm=input("") #代码开始 zn = ord(zm) #67 k = zn-65 for i in range(0,k+1): print(' '*(k-i),end='') for j in range(0,i+1): print(chr(65+j),end='') for n in range(i,0,-1): print(chr(64+n),end='') print('\n',end='') print('') #代码结束
-
逻辑推断
#代码开始 for a in range(0,2): for b in range(0,2): for c in range(0,2): for d in range(0,2): if a+b+c+d == 1: #只有一个罪犯 if (b==1 or c==1 or d==1)+2*(b==0 and c==1)+(a==1 or d==1)==2: if a == 1: print('A是罪犯') elif b == 1: print('B是罪犯') elif c == 1: print('C是罪犯') elif d == 1: print('D是罪犯') #代码结束
-
多人唐诗作品
import re f1=open("sy6//唐诗.txt","r",encoding="utf-8") #代码开始 content = f1.read() #只读一次 while True: x=input("诗人姓名") def cha(i): t = re.findall("\d{3}"+i+'\:\S+',content) if i != '' and len(t) == 0: print("无作品") elif len(t) > 0: for k in t: print(k) print('一共'+str(len(t))+'首') cha(x) if x == '': break #代码结束 f1.close()
更多推荐
已为社区贡献3条内容
所有评论(0)