求尾数0的个数

来源:互联网 发布:志怪故事 知乎 编辑:程序博客网 时间:2024/04/30 20:39

/*

*求阶乘的尾数的零的个数:100!的尾数中有多少个0


*/

//分析:尾数0的个数也只跟乘数与被乘数的最后一位数相关,若是改变了数值,则要改变测试末尾0个数的方式

#include<iostream>

#include<cmath>

using namespace std;

int main()

    {
     int h=1,n=99;

     int count=2;//开始从99

     int m=0;
    
     while(n!=1)

         {
           m=h*n;//记录,为了设置在乘积后有0情况下的h值

          h=h*n%10;//保留最后一位尾数

          if(h==0)

              {
               count++;

               h=m/10 %10;//乘积中出现0

               if(m%100==0)//乘积中出现双0,设置h的值

                   {

                   h=m/100;

                   count++;//加另一个0

                   }

        
              }

          n=n-1;

         }
    
     cout<<"the results is:"<<count<<endl;

     cout<<"h="<<h<<endl;
    
    return 0;}



//数学特征分析:若一个数的因子中含有5,则一定会出现0,因此,就转化为求1~100中含有多少个因子5,若是改变了数,则求因子5的方法也要改变

#include<iostream>

using namespace std;

int main()

    {
      int n;

      int count=0;

      for(n=5;n<=100;n+=5)

          {

          count++;

          if(n%25==0)//25的倍数中含有2个因子5,所以要再加一次

              count++;

          }

      cout<<"the result is: "<<count<<endl;
    
    return 0;}
0 0
原创粉丝点击