目录
P5735 【深基7.例1】距离函数
from math import sqrt
def hanshu(a,b):
return sqrt(pow(a[0]-b[0],2)+pow(a[1]-b[1],2))
ls=[]
cnt=0
for i in range(3):
a,b=map(eval,input().rstrip().split())
ls.append([a,b])
cnt+=hanshu(ls[0],ls[1])
cnt+=hanshu(ls[1],ls[2])
cnt+=hanshu(ls[2],ls[0])
print("%.2f"%(cnt))
P5736 【深基7.例2】质数筛
from math import sqrt
def hanshu(n):
ls,x,y=[True]*(n+1),2,int(sqrt(n))+1
while x<=y:
if ls[x]==True:
for i in range(x*2,n+1,x):
ls[i]=False
x+=1
ls=[i for i in range(2,n) if ls[i]==True]
return ls
n=int(input())
m=[int(i) for i in input().split()]
ls=hanshu(max(m)+1)
for i in m:
if i in ls:
print(i,end=" ")
P5737 【深基7.例3】闰年展示
def hanshu(n):
if n%400==0:
return True
elif n%100==0:
return False
elif n%4==0:
return True
else:
return False
x,y=map(int,input().split())
ls=[]
for i in range(x,y+1):
if hanshu(i)==True:
ls.append(str(i))
print(len(ls))
print(" ".join(ls))
P5738 【深基7.例4】歌唱比赛
a,b=map(int,input().split())
x=-float("inf")
for i in range(a):
ls=[int(i) for i in input().rstrip().split()]
cnt=(sum(ls)-max(ls)-min(ls))/(b-2)
if cnt>x:
x=cnt
print("{:.2f}".format(x))
P5739 【深基7.例7】计算阶乘
n=int(input())
def hanshu(n):
if n==0:
return 1
else:
return n*hanshu(n-1)
print(hanshu(n))
P5461 赦免战俘
n=int(input())
n=pow(2,n)
ls=[["1" for i in range(n)] for i in range(n)]
def hanshu(a,b,n):
global ls
if n!=1:
m=n//2
for i in range(n):
for j in range(n):
if i<m and j<m:
ls[a+i][b+j]="0"
n=m
c=a+n
d=b+n
hanshu(c,b,n)
hanshu(a,d,n)
hanshu(c,d,n)
hanshu(0,0,n)
for i in ls:
print(" ".join(i))
P5740 【深基7.例9】最厉害的学生
n=int(input())
di={}
for i in range(n):
ls=[i for i in input().split()]
cnt=0
for i in ls:
try:
cnt+=int(i)
except:
pass
if cnt not in di:
di[cnt]=ls
print(" ".join(di[max(di.keys())]))
P5741 【深基7.例10】旗鼓相当的对手 - 加强版
def hanshu(i,j):
cnt1,cnt2=0,0
for k in range(1,4):
a,b=int(i[k]),int(j[k])
if abs(a-b)>5:
return False
cnt1+=a
cnt2+=b
else:
if abs(cnt1-cnt2)<=10:
return True
n=int(input())
ls=[]
for i in range(n):
sl=[i for i in input().split()]
ls.append(sl)
sl=[]
for i in range(n):
for j in range(i+1,n):
if hanshu(ls[i],ls[j])==True:
mn=[ls[i][0],ls[j][0]]
mn.sort()
sl.append(mn)
sl.sort()
for i in sl:
print(" ".join(i))
P5742 【深基7.例11】评等级
n=int(input())
for i in range(n):
ls=[int(i) for i in input().split()]
a,b=sum(ls[1:]),int(ls[1]*0.7+ls[2]*0.3)
if b>=80 and a>140:
print("Excellent")
else:
print("Not excellent")
P1075 [NOIP2012 普及组] 质因数分解
from math import sqrt
def hanshu(n):
ls,x,y=[True]*(n+1),2,int(sqrt(n))+1
while x<=y:
if ls[x]==True:
for i in range(x*2,n+1,x):
ls[i]=False
x+=1
ls=[i for i in range(2,n) if ls[i]==True]
return ls
n=int(input())
ls=hanshu(100000)
for i in ls:
if n%i==0:
j=n/i
print("{:.0f}".format(max(i,j)))
break
P1304 哥德巴赫猜想
from math import sqrt
def hanshu(n):
m=int(sqrt(n))+1
for i in range(2,m):
if n%i==0:
return False
return True
N=int(input())
for i in range(4,N+1,2):
x=(i-1)//2+2
for j in range(2,x):
if hanshu(j)==True and hanshu(i-j)==True:
print("{}={}+{}".format(i,j,i-j))
break
P1217 [USACO1.5]回文质数 Prime Palindromes
代码一:
from math import sqrt
def hanshu(x,y,z):
global ls,n,m
if y==x:
for i in ["1","3","5","7","9"]:
z+=i
hanshu(x,y-1,z)
z=z[:-1]
elif y==0:
j=int(z[1:][::-1]+z)
if j>=n and j<=m and zhishuhanshu(j)==True:
ls.append(j)
else:
for i in ["0","1","2","3","4","5","6","7","8","9"]:
z=i+z
hanshu(x,y-1,z)
z=z[1:]
def huiwenhanshu1(x):
x=x//2+1
hanshu(x,x,"")
def zhishuhanshu(n):
m=int(sqrt(n))+1
for i in range(2,m):
if n%i==0:
return False
return True
n,m=map(int,input().split())
x,y,ls=len(str(n)),len(str(m)),[]
for i in range(x,y+1):
if i%2==1:
huiwenhanshu1(i)
else:
if i==2:
if 11>=n and 11<=m:
ls.append(11)
ls.sort()
for i in ls:
print(i)
注释:这个完全AC。先生成回文数列表。然后暴力判断质数。
代码二:
from math import sqrt
def hanshu(a,b):
ls=[True]*(b+1)
c=int(sqrt(b))+1
d=2
while d<c:
if ls[d]==True:
for i in range(d+d,b+1,d):
ls[i]=False
d+=1
for i in range(a,b+1):
j=str(i)
if j==j[::-1]:
if ls[i]==True:
print(j)
a,b=map(int,input().split())
hanshu(a,b)
注释:这个使用欧拉筛法。虽然已经走到尽头。但是还是不能过哦。基数太大了啊。
P2415 集合求和
ls=[int(i) for i in input().split()]
n=len(ls)
n=pow(2,(n-1))
cnt=0
for i in ls:
cnt+=n*i
print(cnt)
注释:这个一看就不能用暴力循环解决问题,直接使用去推导公式。好推得很。
P5743 【深基7.习8】猴子吃桃
x=1
y=int(input())
z=0
while z<y-1:
x+=1
x*=2
z+=1
print(x)
P5744 【深基7.习9】培训
n=int(input())
for i in range(n):
ls=[i for i in input().rstrip().split()]
ls[2]=int(ls[2])
ls[2]*=1.2
if ls[2]>=600:
ls[2]=600
print(ls[0],int(ls[1])+1,int(ls[2]))