- 一、三角形类型
- 读入数组并排序的方法
- n=list(map(float,input().split())) c,b,a=sorted(n)
- list_1 = list(map(float, input().split())) list_1.sort() list_1.reverse()
- lengths=sorted(map(float,input().split(" ")),reverse=True)
- 二、动物
- 写法一:
- d={" ":{" "}," ":{" "}}
- 写法二:嵌套的if
- 写法三:index()
- 三、菱形
- 四、质数
- 五、完全数
- 六、数字序列和它的和
- 写法一:
- 写法二:
- 七、连续奇数的和2
- 写法一:我写复杂了,直接暴力就可以了
- 写法二:
- for j in range(a[i*2]+1,a[i*2+1],1):
- 写法三:暴力法
- 八、实验
- 字典
- 九、蛇形矩阵
一、三角形类型
n=list(map(float,input().split()))
c,b,a=sorted(n)
if a>=b+c:
print("NAO FORMA TRIANGULO")
exit()
if a**2==b**2+c**2:
print("TRIANGULO RETANGULO")
elif a**2>b**2+c**2:
print("TRIANGULO OBTUSANGULO")
elif a**2<b**2+c**2:
print("TRIANGULO ACUTANGULO")
if a==b==c:
print("TRIANGULO EQUILATERO")
elif a==b or a==c or b==c:
print("TRIANGULO ISOSCELES")
读入数组并排序的方法
n=list(map(float,input().split())) c,b,a=sorted(n)
list_1 = list(map(float, input().split())) list_1.sort() list_1.reverse()
lengths=sorted(map(float,input().split(" ")),reverse=True)
二、动物
写法一:
d={" “:{” “},” “:{” "}}
这是一个嵌套的字典数据结构,包含了动物分类以及不同分类的动物名称。可以通过字典的嵌套结构来获取动物名称。
d = {"vertebrado":{"ave":{"carnivoro":"aguia","onivoro":"pomba"},"mamifero":{"onivoro":"homem","herbivoro":"vaca"}},"invertebrado":{"inseto":{"hematofago":"pulga","herbivoro":"lagarta"},"anelideo":{"hematofago":"sanguessuga","onivoro":"minhoca"}}}
a,b,c = input(),input(),input()
print(d[a][b][c])
写法二:嵌套的if
s1 = input()
s2 = input()
s3 = input()
if s1 == "vertebrado":
if s2 == "ave":
if s3 == "carnivoro":
print("aguia")
else:
print("pomba")
else:
if s3 == "onivoro":
print("homem")
else:
print("vaca")
else:
if s2 == "inseto":
if s3 == "herbivoro":
print("lagarta")
else:
print("pulga")
else:
if s3 == "onivoro":
print("minhoca")
else:
print("sanguessuga")
写法三:index()
one = ["vertebrado","invertebrado"]
two = ["ave","mamifero","inseto","anelideo"]
three = ["carnivoro","onivoro","herbivoro","hematofago"]
a = one.index(input())
b = two.index(input())
c = three.index(input())
d = {"000":"aguia","001":"pomba","011":"homem","012":"vaca","123":"pulga",
"122":"lagarta","133":"sanguessuga","131":"minhoca"}
# print(a,b,c)
print(d[str(a)+str(b)+str(c)])
三、菱形
n = int(input())
c = n // 2
for i in range(n):
for j in range(n):
if abs(i - c) + abs(j - c) <= c:
print('*', end = '')
else:
print(' ', end = '')
print()
四、质数
n=int(input())
for i in range(n):
x=int(input())
flag=True
for i in range(2,int(x**0.5)+1):
if x%i==0:
flag=False
break
if flag:
print("%d is prime"%x)
else:
print("%d is not prime"%x)
五、完全数
n=int(input())
for i in range(n):
ans=0
x=int(input())
for j in range(1,x):
if j**2>x:#剪枝,我一开始没有这句话tle了
break
if x%j==0:
if j<x:
ans+=j
if j!=x/j and x/j<x:#如果i**2不等于x
ans+=int(x/j)
if ans==x:
print("%d is perfect"%x)#print("{} is perfect".format(x))
else: print("%d is not perfect"%x)
六、数字序列和它的和
写法一:
while True:
a, b = map(int, input().split())
if a<=0 or b<=0:
exit()
if a>b:
a,b=b,a
sum=0
for i in range(a,b+1):
print(i,end=' ')
sum+=i
print("Sum={}".format(sum))
写法二:
while True:
a,b=map(int,input().split(' '))
if a<=0 or b <= 0:
break
y=max(a,b)
x=min(a,b)
sum=0
for i in range(x,y+1):
print(i,end=' ')
sum+=i
pass
print("Sum=%d"%sum)
七、连续奇数的和2
写法一:我写复杂了,直接暴力就可以了
然后要注意的就是数组要开大一点,数组的创建和读入的方法。
a=[0]*1005
a[0]=0
for i in range(1,1005):
if (i-1)&1:
a[i]=a[i-1]+i-1
else:
a[i]=a[i-1]
n=int(input())
for _ in range(n):
d,b=map(int,input().split())
if d>b:
d,b=b,d
if d==b:
print(0)
elif d>0 and b>0:
print(a[b]-a[d+1])
elif d<0 and b<0:
print(-a[-d]+a[-b+1])
elif d<0 and b>0:
print(-a[-d]+a[b])
写法二:
for j in range(a[i2]+1,a[i2+1],1):
这里的 range() 函数使用三个参数的形式,其中第一个参数是循环起始值,第二个参数是循环结束值(不包含),第三个参数是循环步长。因此,range(a[i2]+1, a[i2+1], 1) 表示从 a[i2]+1 开始,每次增加 1,直到 a[i2+1],循环结束。
举个例子:
a = [[0, 5], [10, 15], [20, 25]]
for i in range(len(a)):
for j in range(a[i][0] + 1, a[i][1], 1):
# 在这里写需要执行的操作,例如打印 j 的值
print(j)
这段代码会依次打印出 1 到 4 和 11 到 14 和 21 到 24 这些数字,因为它们分别属于 a 中的三个区间。
n = int(input())
a = []
for i in range(n):
b = [int(x) for x in input().split()]
a = a + b#把他一个个存入到a中
for i in range(n):
count = 0
if a[i*2] > a[i*2+1]:
p = a[i*2]
a[i*2] = a[i*2+1]
a[i*2+1] = p
for j in range(a[i*2]+1,a[i*2+1],1):
if j % 2 == 1:
count += j
print(count)
感觉这个写法的思想很巧妙!
写法三:暴力法
n=int(input())
for i in range(n):
s=0
x,y=map(int,input().split())
if x>y:x,y=y,x
for j in range(x+1,y):
if j%2==1:
s+=j
print(s)
八、实验
字典
这里使用了字典!!!
n = int(input())
res = 0
data = {}
for i in range(n):
a,b = input().split(" ")
# print(a,b)
a = int(a)
if data.get(b,False):
data[b] = data[b] + a
else:
data[b] = a
res+=a
print("Total: {} animals".format(res))
print("Total coneys: {}".format(data["C"]))
print("Total rats: {}".format(data["R"]))
print("Total frogs: {}".format(data["F"]))
print("Percentage of coneys: {:.2f} %".format(data["C"]/res*100))
print("Percentage of rats: {:.2f} %".format(data["R"]/res*100))
print("Percentage of frogs: {:.2f} %".format(data["F"]/res*100))
九、蛇形矩阵
在学C++的时候经常写,有点生了,写了好一会
n,m=map(int,input().split())
dx=[0,1,0,-1]
dy=[1,0,-1,0]
a=[[0 for j in range(m)] for i in range(n)]
x,y,t=0,0,0
for i in range(1,n*m+1):
a[x][y]=i
x1,y1=x+dx[t],y+dy[t]
if x1<0 or x1>=n or y1>=m or y1<0 or a[x1][y1]:
t=(t+1)%4
x1,y1=x+dx[t],y+dy[t]
x,y=x1,y1
for i in range(n):
for j in range(m):
print(a[i][j],end=' ')
print()