Python3语法入门题题解 & AcWing Django框架课
【摘要】
Python3语法入门题题解 & AcWing Django框架课
文章目录
Python3语法入门题题解 & AcWing Django框架课AcWing 608. 差AcWi...
Python3语法入门题题解 & AcWing Django框架课
AcWing 608. 差
输入abcd,输出ab-cd
# 循环输入
a,b,c,d = (int(input()) for i in range(4))
print("DIFERENCA =",a*b-c*d)
- 1
- 2
- 3
AcWing 665. 倍数
输入ab,判断是否有倍数关系
# split输入
a, b = map(int, input().split()) # split按空格和\n分割字符串, map转int
if b % a == 0 or a % b == 0:
print("Sao Multiplos")
else:
print("Nao sao Multiplos")
- 1
- 2
- 3
- 4
- 5
- 6
AcWing 670. 动物
给出一个json数据,求输出某个叶节点的值
# 字典嵌套
t1={"carnivoro":"aguia","onivoro":"pomba"}
t2={"onivoro":"homem","herbivoro":"vaca"}
t3={"hematofago":"pulga","herbivoro":"lagarta"}
t4={"hematofago":"sanguessuga","onivoro":"minhoca"}
r1={"ave":t1,"mamifero":t2}
r2={"inseto":t3,"anelideo":t4}
s={"vertebrado":r1,"invertebrado":r2}
a,b,c=input(),input(),input() # input每次读入一行
print(s[a][b][c])
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
AcWing 660. 零食
给出 5 种零食的价目表。
求某种零食x数量为y时需要的钱。
# list输入
xy=list(map(int,input().split())) # map返回迭代器,list转为列表
print(f'Total: R$ {(4.00,4.50,5.00,2.00,1.50)[xy[0]-1]*xy[1]:.2f}')
- 1
- 2
- 3
AcWing 760. 字符串长度
给出一行含空格的字符串,求长度
print(len(input()))
- 1
AcWing 721. 递增序列
对于输入的整数n,输出1,2,3,n的序列
n=0时结束程序
n = int(input())
while n:
for i in range (n):
print(i+1, end = " ")
print()
n = int(input())
- 1
- 2
- 3
- 4
- 5
- 6
AcWing 726. 质数
输入n个数,判断是不是质数
import math
n = int(input())
for i in range(n):
x = int(input())
s = math.floor(math.sqrt(x))
p = True
for j in range(2, s+1):
if x%j==0 :
p = False
break
if p:
print(x, "is prime")
else:
print(x, "is not prime")
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
AcWing 745. 数组的右上半部分
给出一个12*12的矩阵,求右上半部分的平均值和元素和
op = input()
a = []
for i in range(12):
a.append(list(map(float, input().split())))
res = 0
for i in range(12):
for j in range(i+1, 12):
res += a[i][j]
if(op=='M'): res/=66
print("%.1f"%(res))
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
AcWing 756. 蛇形矩阵
给出n和m,将1-nm的数字按照回字蛇形填充至矩阵中
nm = input().split()
n = int(nm[0])
m = int(nm[1])
dx = [0,1,0,-1] # 按顺时针顺序走
dy = [1,0,-1,0]
res = [[0]*m for _ in range(n)] # 初始化数组
x,y,a,b,d = 0,0,0,0,0
for k in range(1,n*m+1):
res[x][y] = k
a=x+dx[d] #每次走一步
b=y+dy[d]
if a<0 or a>=n or b<0 or b>=m or res[a][b]!=0: #判断合法
d=(d+1)%4
a=x+dx[d]
b=y+dy[d]
x = a
y = b
for i in range(n):
for j in range(m):
print(f"{res[i][j]} ", end="")
print()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
AcWing 823. 排列
给出n,求1-n的全排列
def dfs(cur):
if cur > n:
for i in range(1,n+1):
print(a[i], end=" ")
print()
else :
for i in range(1,n+1):
if(vis[i] == False):
vis[i] = True
a[cur] = i
dfs(cur+1)
vis[i] = False
n = int(input()) #输入
a = [0]*(n+1) #声明数组
vis = [False]*(n+1)
dfs(1)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
文章来源: gwj1314.blog.csdn.net,作者:小哈里,版权归原作者所有,如需转载,请联系作者。
原文链接:gwj1314.blog.csdn.net/article/details/125474520
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)