輸入abcd,輸出ab-cd
# 循環輸入
a,b,c,d = (int(input()) for i in range(4))
print("DIFERENCA =",a*b-c*d)
輸入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")
給出一個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])
給出 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}')
給出一行含空格的字符串,求長度
print(len(input()))
對於輸入的整數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())
輸入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")
給出一個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))
給出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()
給出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)