如下的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
int main()
{
int count2 = 0, count5 = 0;
int num;
for (int i = 0; i < 100; i++)
{
cin >> num;
while (num % 5 == 0)
{
count5++;
num %= 5;
}
while (num % 2 == 0)
{
count2++;
num %= 2;
}
}
int ans = count2 < count5 ? count2 : count5;
cout << ans;
return 0;
}
int a[] = {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};
int main()
{
int k1 = 0, k2 = 0;
for (int i = 0; i < 100; i++)
{
while (!(a[i] % 2))
a[i] /= 2, k1++;
while (!(a[i] % 5))
a[i] /= 5, k2++;
}
cout << min(k1, k2);
return 0;
}
int main()
{
int t_cnt, f_cnt, data = 0;
t_cnt = f_cnt = 0;
for (int i = 0; i < 100; i++)
{
cin >> data;
while (data % 5 == 0)
{
f_cnt++;
data /= 5;
}
while (data % 2 == 0)
{
t_cnt++;
data /= 2;
}
}
cout << min(t_cnt, f_cnt) << endl;
return 0;
}
int main()
{
long long int a, sum = 1, aum = 0;
for (int i = 0; i < 100; i++)
{
cin >> a;
sum = sum * a;
while ((sum % 10) == 0)
{
sum = sum / 10;
aum++;
}
sum = sum % 10000;
}
printf("%lld", aum);
return 0;
}
4种代码中第一种取余的写法是错的,为什么?
通过观察可知,需要 末尾为 5 与 末尾为 2、4、6、8 的数字相乘,而这样的数字可拆分为多个 2 与 5 相乘,
例如 125 * 512 = 64000, 125 = 5^3, 512 = 2^9 ,于是 2 的个数为 9,5 的个数为 3,共有 3 对 2 * 5,所以结果为 3。
举例说明:100是2个0,而因为%=操作只算作1个