题目描述:如下的10行数据,每行有10个整数,请你求出它们的乘积的末尾有多少个零?
5650 4542 3554 473 946 4114 3871 9073 90 4329
2758 7949 6113 5659 5245 7432 3051 4434 6704 3594
9937 1173 6866 3397 4759 7557 3070 2287 1453 9899
1486 5722 3135 1170 4014 5510 5120 729 2880 9019
2049 698 4582 4346 4427 646 9742 7340 1230 7683
5693 7015 6887 7381 4172 4341 2909 2027 7355 5649
6701 6645 1671 5978 2704 9926 295 3125 3878 6785
2066 4247 4800 1578 6652 4616 1113 6205 3264 2915
3966 5291 2904 1285 2193 1428 2265 8730 9436 7074
689 5510 8243 6114 337 4096 8199 7313 3685 211
本题为填空题,只需要算出结果后,在代码中使用输出语句将所填结果输出即可。
思路:只有2和2的倍数与5或5的倍数相乘才会出现0。(2x5=10,2x25=100)于是找出矩阵中可将2或者5整除的数的次数并统计数量,对比数量,数量少的部分则为0的个数。
一共有31个0
import os
import sys
# 请在此输入您的代码:
list=[5650, 4542, 3554, 473, 946, 4114, 3871, 9073, 90, 4329,
2758, 7949, 6113, 5659, 5245, 7432, 3051, 4434, 6704, 3594,
9937, 1173, 6866, 3397, 4759, 7557, 3070, 2287, 1453, 9899,
1486, 5722, 3135, 1170, 4014, 5510, 5120, 729, 2880, 9019,
2049, 698, 4582, 4346, 4427, 646, 9742, 7340, 1230, 7683,
5693, 7015, 6887, 7381, 4172, 4341, 2909, 2027, 7355, 5649,
6701, 6645, 1671, 5978, 2704, 9926, 295, 3125, 3878, 6785,
2066, 4247, 4800, 1578, 6652, 4616, 1113, 6205, 3264, 2915,
3966, 5291, 2904, 1285, 2193, 1428, 2265, 8730, 9436, 7074,
689, 5510, 8243, 6114, 337, 4096, 8199, 7313, 3685, 211]
# 将列表转换为一个 10x10 的矩阵
matrix = [list[i:i+10] for i in range(0, len(list), 10)]
sum2 = sum5 = 0
for i in range(10):
for j in range(10):
while matrix[i][j]%2==0:
matrix[i][j] = matrix[i][j] / 2
sum2 += 1
if matrix[i][j] % 2 != 0:
break
while matrix[i][j]%5==0:
matrix[i][j] = matrix[i][j] / 5
sum5 += 1
if matrix[i][j] % 5 != 0:
break
print(min(sum2, sum5))
import os
import sys
# 请在此输入您的代码:
count =0
num2 = num5=0
list =[5650, 4542, 3554, 473, 946, 4114, 3871, 9073, 90, 4329,
2758, 7949, 6113, 5659, 5245, 7432, 3051, 4434, 6704, 3594,
9937, 1173, 6866, 3397, 4759, 7557, 3070, 2287, 1453, 9899,
1486, 5722, 3135, 1170, 4014, 5510, 5120, 729, 2880, 9019,
2049, 698, 4582, 4346, 4427, 646, 9742, 7340, 1230, 7683,
5693, 7015, 6887, 7381, 4172, 4341, 2909, 2027, 7355, 5649,
6701, 6645, 1671, 5978, 2704, 9926, 295, 3125, 3878, 6785,
2066, 4247, 4800, 1578, 6652, 4616, 1113, 6205, 3264, 2915,
3966, 5291, 2904, 1285, 2193, 1428, 2265, 8730, 9436, 7074,
689, 5510, 8243, 6114, 337, 4096, 8199, 7313, 3685, 211]
for i in range (100):
while list[i]%2 ==0:
num2 +=1
list[i]=list[i]/2
if list[i]%2 !=0:
break
while list[i]%5 ==0:
num5 +=1
list[i]=list[i]//5
if list[i]%5!=0:
break
print(min(num2,num5))