标题:乘积尾零
如下的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
注意:需要提交的是一个整数,表示末尾零的个数。不要填写任何多余内容。
我们对每一个数进行因式分解,假设我们有n个数,统计这n个数的质因子中一共有多少
个2和5;
显然我们一个2和一个5能组成一个10,我们要知道这些2和5能组成多少个10即可;
答案是 31
代码如下
#include<stdio.h>
int data[] ={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};
struct node
{
int a2 , a5; //用来储存每个数质因子中2和5的个数;
};
node count(int num); //计算num 质因子中2和5的数量,并存在结构体中
int main()
{
int len = sizeof(data) / sizeof(int);
int a5 = 0,a2 = 0;
for(int i = 0;i<len;i++)
{
a2 += count(data[i]).a2;
a5 += count(data[i]).a5;
}
printf("%d\n",a2 < a5 ? a2 : a5);
return 0;
}
node count(int num)
{
node temp;
for(int i = 2; i * i < num; i++)
{
int result = 0;
while(num % i == 0)
{
result++;
num /= i;
}
if(i == 2)
temp.a2=result;
else if(i == 5)
temp.a5=result;
}
if(num!=1)
{
if(num == 2)
temp.a2 = 1;
else if(num == 5)
temp.a5 = 1;
}
return temp;
}