目录
1.卡片(1443)
错解:
a = [2021 for i in range(10)] #存储10种卡片的数量
cnt = 1
#遍历各位数
while a[1]>0:
while cnt>0:
x= cnt%10
a[x] = a[x]-1
cnt //=10
cnt +=1
print(cnt-1)
只会输出0
原因分析:
while循环中出来了的cnt=0,再加上1后cnt为,1,经第二层while循环又会变为0,最后输出cnt = 1
#cnt的值始终在变化,出循环后cnt的值不再是进循环的值
改进方法:
1.利用函数局部变量的性质(函数中改变的列表,函数外的列表也改变)
a = [2021 for i in range(10)]
cnt = 1
#遍历各位数
def check(x):
#依次读取各位数的数值
while x>0:
n = x%10
if a[n]>0:
a[n] -=1
x //=10
else:
return 0 #退出循环
return 1 #输出True
cnt = 1
while check(cnt):
cnt +=1
print(cnt -1) #注意减一
2. 利用excel:
先估计可能可以拼出3000多个数,excel输入1~3500,可利用excel的功能查询1的使用次数(1使用次数最多,优先用完)
2.外卖店优先(184)
错误部分:
存储时将列表生成器敲成了生成器对象(generator object)
错解:
n,m,T = map(int,input().split())
a = []
priority = []
for i in range(m):
a.append(int(j) for j in input().split()) #append()内未加[],生成生成器
print(a)
绿色字体为输入,白色为输出
正解:
n,m,T = map(int,input().split())
a = []
priority = []
for i in range(m):
a.append([int(j) for j in input().split()])
print(a) #检验a
绿色字体为输入,白色为输出