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的所有完全平方數(shù)'''
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)
代碼都寫出來了,不至于看不懂吧,最后結(jié)果為
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)名參賽運動員,
已經(jīng)修改好了: #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)
##測試結(jié)果如下圖
Python比賽評分計算代碼編寫,題目如圖,不會麻煩不要答,會停止推送!
n=int(input('請輸入總共幾名評委:'))
li=[]
foriinrange(n):
li.append(float(input('請輸入第%d名評委評分:'%(i+1))))
print('該歌手最終成績?yōu)椋?+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("輸入一個正偶數(shù):"))
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("輸入數(shù)據(jù)出錯!")
源代碼(注意源代碼的縮進)
上一篇
多少年的中央一號文件與農(nóng)業(yè)有關(guān)
下一篇
返回列表