Python字典练习【第九周】
六、金币排列
n=eval(input())
a=[]
j,k=-1,-1
cnt,res=0,0
for i in range(1,n):
a.append(i)
while True:
j+=1
cnt=a[j]+cnt
if(cnt>=n):
break
if(sum(a[:3])==n):
print(j+1,a[0])
else:
m=cnt-n
while True:
k+=1
res=a[k]+res
if(res>=m):
break
p=res-m
if(p==0):
print(j-k,a[k+1])
else:
print(j-k+1,p)
七、歌咏小能手
n=eval(input())
a= list(map(int,input().split(",")))
a.remove(max(a))
a.remove(min(a))
sum = 0
for i in a:
sum += i
ave = float(sum/(n-2))
cnt = 0
for i in a:
if i<ave:
cnt+=1
print("此人最终得分是:{:.3f},有{:d}个低于平均值的成绩。".format(ave, cnt))
八、矩阵相加
n=int(input())
a=[]
for i in range(n):
s1=input()
a.append([int(n) for n in s1.split()])
b=[]
for i in range(n):
s2=input()
b.append([int(n) for n in s2.split()])
for i in range(n):
for j in range(n):
t=a[i][j] + b[i][j]
if(j==n-1):
print("{:^3d}".format(t),end=" ")
else:
print("{:^3d}".format(t), end=" ")
print(end="\n")
九、寻找最佳组合
n = eval(input())
s = list(map(int,input().split(",")))
a = []
for i in range(len(s)):
t = []
if s[i]==9:
t.append(s[i])
a.append(t)
for i in range(len(s)):
t = []
flag = 0
for j in range(i+1,len(s)):
if s[i]+s[j]==9 and s[i]!=0 and s[j]!=0:
flag=1
t.append(s[i])
t.append(s[j])
if flag == 1:
a.append(t)
for i in range(len(s)):
for j in range(i+1,len(s)):
t = []
flag = 0
for k in range(j+1,len(s)):
if s[i]+s[j]+s[k]==9 and s[i]!=9 and s[j]!=9 and s[k]!=9:
flag = 1
t.append(s[i])
t.append(s[j])
t.append(s[k])
if flag==1:
a.append(t)
print(a)
十、单词的区域
s = list(input().split())
re = ['q','w','e','a','s','d','z','x','c','Q','W','E','A','S','D','Z','X','C']
bl = ['r','t','y','f','g','h','v','b','n','R','T','Y','F','G','H','V','B','N']
ye =['u','i','o','p','j','k','l','m','U','I','O','P','J','K','L','M']
words={}
pre=[]
for i in s:
cnt=0
for j in i:
if j in re:
cnt +=1
if cnt== len(i):
pre.append(i)
pbl=[]
for i in s:
cnt=0
for j in i:
if j in bl:
cnt +=1
if cnt== len(i):
pbl.append(i)
pye=[]
for i in s:
cnt=0
for j in i:
if j in ye:
cnt +=1
if cnt== len(i):
pye.append(i)
words['Red'] = pre
words['Blue'] = pbl
words['Yellow'] = pye
print(words)
|