乘积尾零
题目描述
本题为填空题,只需要算出结果后,在代码中使用输出语句将所填结果输出即可。
如下的 10 行数据,每行有 10 个整数,请你求出它们的乘积的末尾有多少个零?
的乘积的末尾有多少个零?
5650 4542 3554 473 946 411438719073 90
4329
2758
7949 611356595245 7432 30514434 6704
3594
9937 11736866339747597557307022871453
9899
1486 5722 3135 1170401455105120 729 2880
9019
2049
698 458243464427646974273401230
7683
5693 7015688773814172 43412909 20277355
5649
6701
6645 16715978 2704 9926295 3125 3878
6785
2066
4247 480015786652461611136205 3264
2915
3966 5291 29041285 2193 14282265 8730 9436
7074
689 5510 8243 6114337 4096 8199 7313 3685211
解题思路
题意是让求这一百个数字相乘,乘积会有多少个0,我们应该想到,2*5末尾为0,所以求出改数字对2和对5求余为0的有多少个,最后答案,应该是2和5个数的最小值。
代码如下
#include <bits/stdc++.h>
using namespace std;
int main(){
int cnt2 = 0, cnt5 = 0;
for(int i = 1; i <= 10; i++){
for(int j = 1; j <= 10; j++){
int x;
cin >> x;
while(x % 2 == 0) cnt2++,x/=2;
while(x % 5 == 0) cnt5++,x/=5;
}
}
cout << min(cnt2, cnt5) << "\n";
return 0;
}