[蓝桥杯2018初赛]乘积尾零

题目描述

如下的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 

输出

输出一个整数表示答案
 

题解:尾数产生0的有两种方式,第一个就是尾数本身位0,另一个就是乘数和被乘数同时因数分解,产生2和5相乘,所以统计0的个数,再加上因式分解之后min(2,5)就是答案

 

答案:31

#include<iostream>
#include<string>
#include<algorithm>
#include<math.h>
#include<string.h>
#include<map>
#include<stack>
#include<queue>
#define ll long long
using namespace  std;
int gcd(int a,int b)
{
    return b==0?a:gcd(b,a%b);
}

int main()
{
    int x,ans=0,cnt2=0,cnt5=0;
    for(int i=0;i<10;i++)
    {
        for(int j=0;j<10;j++)
        {
            cin>>x;
            while(x)//
            {
                int temp=x%10;
                if(temp==0)
                {
                    ans++;
                    x=x/10;
                }
                else
                    break;
                
            }
            while(x%2==0)
            {
                cnt2++;
                x=x/2;
            }

            while(x%5==0)
            {
                cnt5++;
                x=x/5;
            }
        }
    }
    // cout<<ans<<endl;
    // cout<<cnt2<<' '<<cnt5<<endl;
    cout<<ans+min(cnt2,cnt5)<<endl;

    return 0;
} 

 

posted @ 2020-02-24 21:56  知道了呀~  阅读(638)  评论(0编辑  收藏  举报