题目描述
如下的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
输出
输出一个整数表示答案
https://blog.csdn.net/weixin_43914593/article/details/112560449
填空,送分。
倪文迪说:“将统计0的个数转化为统计5与2的个数,最终取二者的最小值。”100个数的乘积太大,c++处理不了。联想到10等于2 × 5 2\times52×5,那么只要统计乘积中有多少2和5的因子,特别是5的个数,它一般比2的个数多,所以有多少个5,就有多少个零。
c++代码
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<set>
#include<queue>
#include<stack>
#include<map>
#include<cmath>
using namespace std;
typedef long long ll;
const int maxn = 101000;
#define inf 2147483678
int main()
{
int ct2=0,ct5=0;
for(int i=0;i<10;i++)
{
for(int j=0;j<10;j++)
{
int x;
cin>>x;
while(x&&x%2==0)
{
ct2++;
x/=2;
}
while(x&&x%5==0)
{
ct5++;
x/=5;
}
}
}
cout<<min(ct2,ct5);
return 0;
}
python代码
num=[int(i)for i in input().split()]
s=1
cnt=0
for i in range(len(num)):
s*=num[i]
while s%10==0:
s/=10
cnt+=1
print(cnt)