zoukankan      html  css  js  c++  java
  • SGU 154. Factorial

    154. Factorial

    time limit per test: 0.5 sec.
    memory limit per test: 4096 KB
    input: standard input
    output: standard output



    You task is to find minimal natural number N, so that N! contains exactly Q zeroes on the trail in decimal notation. As you know N! = 1*2*...*N. For example, 5! = 120, 120 contains one zero on the trail.

    Input
    One number Q written in the input (0<=Q<=10^8).

    Output
    Write "No solution", if there is no such number N, and N otherwise.

    Sample test(s)

    Input
    2

    Output
    10

    题目大意:给出一个数n的阶乘n!的末尾0的个数,求n最小是多少,如果找不到输出No solution

    思路分析:由于2和5乘积会出现0,所以可以二分一个范围,是否存在一个数末尾0的个数满足要求


    #include<iostream>
    
    using namespace std;
    
    int cal(int n)
    {
        int res=0;
        while(n)
        {
            res+=n/5;
            n/=5;
        }
        return res;
    }
    
    int main()
    {
        int q;
        while(cin>>q)
        {
            if(q==0)
            {
                cout<<1<<endl;
                continue;
            }
            int right=1000000003,left=1;
            int mid;
            while(left<right)
            {
                mid=(left+right)/2;
                if(cal(mid)<q)
                    left=mid+1;
                else  right=mid;
            }
            if(cal(left)==q) cout<<left<<endl;
            else cout<<"No solution"<<endl;
        }
    }
    


  • 相关阅读:
    认识目标文件的内容
    PetaLinux工程更新HDF文件的脚本
    [海思] 中断申请和重启问题
    [海思] HI3531D串口调试
    【C语言】结构体初始化
    [ZCU106] Petalinux 2018.3 与启动环境搭建记录
    旧版本源码usbip的移植
    jar包下载地址
    Django框架
    前端内容
  • 原文地址:https://www.cnblogs.com/jiangu66/p/3003026.html
Copyright © 2011-2022 走看看