自我练习

2017-08-21 19:38:32 

writer:pprp

/*
theme:第一章 - 分治算法
name:魔法石的诱惑
writer:pprp
description:给你Q(0<=Q<=10^8),问你最小的自然数N使N的阶乘在十进制下包含Q个0
input:Q
output: N
date:Monday 2017/8/21
*/

#include <bits/stdc++.h>

using namespace std;

const int maxn = 500000000;

//判断n的阶乘末尾有多少个0
int judge(int n )
{
    int ans = 0;
    while( n > 0)
    {
        ans += n / 5;
        n = n / 5;
    }
    return ans;
}

void run(int Q)
{
    
    int l = 1;
    int r = maxn;
    int ans = maxn + 1;
    
    while(l <= r)
    {
          int mid = (r + l) >> 1;
          int tmp = judge(mid);
          
          if(tmp == Q) ans = min(mid,ans); 
   
          if(tmp < Q)
          {
                l = mid + 1;
          }
          else if(tmp > Q)
          {
                r = mid - 1;
          }
          else
          {
                r = mid - 1;
          }
    }
      
    if(ans != maxn + 1)
    {
          cout << ans << endl;
    }
    else
    {
          cout << "No solution" << endl;              
    }       
}


int main()
{
    for(int i = 0 ; i < 100 ; i++)
    {
          cout << "i :" << i << endl;
          
          run(i);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/pprp/p/7406266.html