Python如何做一道题某比赛有N(2≤N≤50)名选手参加,给定每名参赛选手的编号(1到N)和
- 教育综合
- 2024-05-11 07:57:19
python问题,跪求大神解答
#!/usr/bin/python
#encoding=utf-8
importsys
defget_candidates(n):
'''返回小于n的所有完全平方数'''
ret=set()
i=2
while(i*i<=n):
ret.add(i*i)
i+=1
returnret
_table=[]
defbt(paired_numbers,player_numbers,candidates):
global_table
forsin_table:
ifplayer_numbers==s:
return
_table.append(player_numbers)
iflen(paired_numbers)==9:
print"findsolution"
printpaired_numbers
sys.exit(0)
forp1inplayer_numbers:
forp2inplayer_numbers:
ifp1>=p2:
continue
if(p1+p2)incandidates:
new_player_numbers=player_numbers.copy()
new_paired_numbers=paired_numbers.copy()
new_player_numbers.remove(p1)
new_player_numbers.remove(p2)
new_paired_numbers.add((p1,p2))
bt(new_paired_numbers,new_player_numbers,candidates)
if__name__=="__main__":
player_numbers=set()
forninrange(1,19):
player_numbers.add(n)
#获取最大可能的号码和
candidates=get_candidates(18+17)
bt(set(),player_numbers,candidates)
代码都写出来了,不至于看不懂吧,最后结果为
set([(3, 13), (6, 10), (7, 18), (1, 15), (9, 16), (4, 12), (8, 17), (5, 11), (2, 14)])
也就是说,1的对手是15
用C语言编程,某次运动会上一共有n(n最大为50)名参赛运动员,
已经修改好了: #include#1
#-*-coding:utf-8-*-
#py3
defperf(n):
#print(n)
s=0
fortinrange(1,int(n/2+1)):
ifn%t==0:
s+=t
ifs==n:
returnTrue
returnFalse
foriinrange(1,1000):
ifperf(i):
print(i)
#-*-coding:utf-8-*-
#py3
n=int(input())
while(n!=1):
print(n,'->',end='')
ifn%2==0:
n=int(n/2)
else:
n=3*n+1
print(n)
##测试结果如下图
Python比赛评分计算代码编写,题目如图,不会麻烦不要答,会停止推送!
n=int(input('请输入总共几名评委:'))
li=[]
foriinrange(n):
li.append(float(input('请输入第%d名评委评分:'%(i+1))))
print('该歌手最终成绩为:'+str((sum(li)-max(li)-min(li))/(n-2)))
一道简单的python编程题?
按照题目要求编写的哥德巴赫猜想的Python程序如下
def IsPrime(v):
if v>=2:
for i in range(2,v//2+1):
if v%i==0:
return False
else:
return True
else:
return False
n=int(input("输入一个正偶数:"))
if n>2 and n%2==0:
for i in range(1,n//2+1):
if IsPrime(i)==True and IsPrime(n-i)==True:
print("%d=%d+%d" %(n,i,n-i))
else:
print("输入数据出错!")
源代码(注意源代码的缩进)
展开全文阅读
上一篇
多少年的中央一号文件与农业有关
下一篇
返回列表