zoukankan      html  css  js  c++  java
  • 阶乘的最后一个非0数

    #include <iostream>
    #include <cstdio>
    //阶乘的最后一个非0数
    //即去掉因子5,2;计算乘积的个位
    //乘积的个位=仅仅对每个数的个位进行乘积,每次都%10即可;连锁起来了
    using namespace std;
    // int main(){
    //     int x,s=0,q=1;
    //     cin>>x;
    //     for(int i=2;i<=x;i++){
    //         int p=i;
    //         while (p%5==0)
    //         {
    //             p=p/5;
    //             s++;
    //         }
    //     }
    //     for(int i=2;i<=x;i++){
    //         int p=i;
    //         while (p%5==0)
    //             p=p/5;
    //         while (p%2==0&&s>0)
    //         {
    //             p=p/2;
    //             s--;
    //         }
    //         q=(q*(p%10))%10;
    //     }
    //     // cout<<s<<endl;
    //     cout<<q<<endl;
    //     return 0;
    // }
    
    //更进一步地,同时对5计数、除以2
    //但是这样必须倒着除
    int main(){
        int x,s=0,q=1;
        cin>>x;
        for(int i=x;i>=2;i--){
            int p=i;
            while (p%5==0)
            {
                p=p/5;
                s++;
            }
            while (p%2==0&&s>0)
            {
                p=p/2;
                s--;
            }
            q=(q*(p%10))%10;
        }
        cout<<q<<endl;
        return 0;
    }
  • 相关阅读:
    Java中抽象类和接口的区别
    servlet的转发与重定向
    JSP知识点
    过滤器与拦截器
    java关键字 super 和 this
    oracle 基础
    java 集合
    java 内部类
    java 数组详解
    图,深度优先遍历与广度优先遍历
  • 原文地址:https://www.cnblogs.com/MorrowWind/p/13056559.html
Copyright © 2011-2022 走看看