当前位置: 首页 > news >正文

学python的第四天---基础(2)

  • 一、三角形类型
    • 读入数组并排序的方法
      • 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=Truefor i in range(2,int(x**0.5)+1):if x%i==0:flag=Falsebreakif flag:print("%d is prime"%x)else:print("%d is not prime"%x)

五、完全数

在这里插入图片描述

n=int(input())
for i in range(n):ans=0x=int(input())for j in range(1,x):if j**2>x:#剪枝,我一开始没有这句话tle了breakif x%j==0:if j<x:ans+=jif j!=x/j and x/j<x:#如果i**2不等于xans+=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,asum=0for i in range(a,b+1):print(i,end=' ')sum+=iprint("Sum={}".format(sum))

写法二:

while True:a,b=map(int,input().split(' '))if a<=0 or b <= 0:breaky=max(a,b)x=min(a,b)sum=0for i in range(x,y+1):print(i,end=' ')sum+=ipassprint("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-1else:a[i]=a[i-1]
n=int(input())
for _ in range(n):d,b=map(int,input().split())if d>b:d,b=b,dif 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 = 0if a[i*2] > a[i*2+1]:p = a[i*2]a[i*2] = a[i*2+1]a[i*2+1] = pfor j in range(a[i*2]+1,a[i*2+1],1):if j % 2 == 1:count += jprint(count)

感觉这个写法的思想很巧妙!

写法三:暴力法

n=int(input())
for i in range(n):s=0x,y=map(int,input().split())if x>y:x,y=y,xfor j in range(x+1,y):if j%2==1:s+=jprint(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] + aelse:data[b] = ares+=aprint("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]=ix1,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)%4x1,y1=x+dx[t],y+dy[t]x,y=x1,y1for i in range(n):for j in range(m):print(a[i][j],end=' ')print()
http://www.lryc.cn/news/32208.html

相关文章:

  • spring之refresh流程-Java八股面试(六)
  • 【C语言】刷题|链表|双指针|指针|多指针|数据结构
  • 糖化学类854262-01-4,Propargyl α-D-Mannopyranoside,炔丙基 α-D-吡喃甘露糖苷
  • 项目管理工具DHTMLX 在 G2 排名中再创新高
  • 28 位委员出席,龙蜥社区第 15 次运营委员会会议顺利召开
  • 自然语言处理-基于预训练模型的方法-chapter3基础工具集与常用数据集
  • 【SpringMVC】@RequestMapping
  • 【深度学习】BERT变体—SpanBERT
  • 根据身高体重计算某个人的BMI值--课后程序(Python程序开发案例教程-黑马程序员编著-第3章-课后作业)
  • 高并发编程JUC之进程与线程高并发编程JUC之进程与线程
  • css基础
  • Unity - 搬砖日志 - BRP 管线下的自定义阴影尺寸(脱离ProjectSettings/Quality/ShadowResolution设置)
  • 如何在SSMS中生成和保存估计或实际执行计划
  • mac 环境下安装MongoDB
  • RTOS中相对延时和绝对延时的区别
  • Solon2 项目整合 Nacos 配置中心
  • Linux 路由表说明
  • MIPI协议
  • 第十届CCF大数据与计算智能大赛总决赛暨颁奖典礼在苏州吴江顺利举办
  • PMP高分上岸人士的备考心得,分享考试中你还不知道的小秘密
  • ubuntu下编译libpq和libpqxx库
  • ESP-C2系列模组开发板简介
  • linux权限管理
  • 提高生活质量,增加学生对校园服务的需求,你知道有哪些?
  • Antlr4:使用grun命令,触发NoClassDefFoundError
  • 基于rootfs构建Docker镜像
  • 电脑文件软件搬家迁移十大工具
  • 【数据库】排名问题
  • 【redis学习篇】主从哨兵集群架构详解
  • 基于jdk8的HashMap源码解析