1, 头部注释
#!/usr/bin/env python
# -*- coding: utf-8 -*-
2, 输入输出, 若使用python2需要将所有的input()改为raw_input()
#多组输入1
while 1:
try:
a, b = map(int,input().split())
print(a+b)
except:
break
#多组输入2
t = int(input())
for i in range(t):
a, b = map(int,input().split())
print(a+b)
#格式化输出
a, b, c = 1, 2, 3
print("a=%d,b=%d,c=%d"%(a,b,c))
3, 数组
#定义一个长度为10全为0的数组
a = [0 for i in range(10)]
#复制数组, 不能用b = a
b = list(a)
#二维数组
c = [[0 for i in range(10)] for j in range (10)]
#数组多行读入
d = [int(input()) for i in range(5)]
#数组单行读入
e = list(map(int,input().split()))
#数组输出
print(a)
#去掉外层括号输出
print(' '.join('%s'%i for i in a))
4, 数组切片
s = "0123456789"
print(s[2:8]) #[2,8)
#234567
print(s[2:8:2]) #[2,8)中每两个取1个
#246
print(s[8:2:-1]) #(2,8]逆序
#876543
print(s[:]) #左侧默认为0, 右侧默认为len
#0123456789
print(s[-8:9]) #下标为-x表示len-x
#2345678
print(s[2:9]) #与s[-8:9]等价
#2345678
练习1, Luogu P1018
大意: 给定长度为$n$的数字串, 求用$k$乘号划分成$k+1$个部分使得乘积最大.
while 1:
try:
n, m = map(int,input().split())
s = str(input())
dp = [[0 for _ in range(100)] for _ in range(100)]
dp[0][0] = 1
for i in range(n+1):
for j in range(i):
for k in range(m+1):
dp[i][k+1] = max(dp[i][k+1], dp[j][k]*int(s[j:i]))
print(dp[n][m+1])
except:
break
练习2,
大意: