【PY随笔】防忘记录
1. for循环的i的增加严格+1,需要变化的话得用while
2. 针对没有告诉具体结束次数,用try except
while True:
try:
n, m = map(int, input().split())
stu = list(map(int, input().split()))
stu.sort()
num = 0
i = 0
## for 循环内部完全不管你改变了顺序,继续在+1地做循环
while i < n-1:
if stu[i+1]-stu[i] < m:
num = num + 1
i = i + 2
else:
i = i + 1
print(num)
except:
exit()
3. 字符串的左补0
m = '%0500d' % n # 500位,若不满则左补0
4. 输出全排列
import itertools
def strls(ls):
s = ''
for i in ls:
s = s + i
return s
n = int(input())
ls = list(input().split()) #得到一个列表
cls = list(itertools.permutations(ls)) #cls是ls全部排列的总列表集合([]里有[])
tmpls = strls(cls[0])
for i in cls:
cur = strls(i)
if cur > tmpls:
tmpls = cur
print(tmpls)
5.四舍五入
print(str(int(jg/7*100+0.5)) + '%') # 四舍五入-》+0.5 后截断