二分查找——zznuoj1906yifan and Zeroes 解题题解

最简单二分查找,分析题意,某数阶乘的末尾有几个零,然而其实就是求阶乘展开式有5的几次方,一个5代表一个零。而且规律可得n个零时,5*n的阶乘中5的x次方大于等于n,顾二分初始边界取1和n,然后不断通过mid来判断,最后缩小范围然后确定。

题目:

问题 F: yifan and Zeroes


题目描述

 One of My Teammates asd encountered a difficult problem recently. Given an integer q, he wants to now whether there is an integer n that n! contains q zeroes on the trail in decimal notation and n is minimal.

输入

Input starts with an integer T(T <= 20000),denoting the number of test cases.   Each test case contains an integer q (1 <= q <= 10^8)

输出

For each case, print the case number and your answer, if no solution is found then print "I need anwei".

样例输入

3
1
2
5

样例输出

Case 1: 5
Case 2: 10
Case 3: I need anwei


#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std; 
int s(int a)
{
	int b=0;
	a *= 5;
	while(a > 0)
	{
		b += a/5;
		a /= 5;
	}
	return b;
}
int main()
{
	int t;
	while(~scanf("%d", &t))
	{
		for(int j = 1; j <= t; j++)
		{
			int p;
			scanf("%d", &p);
			int l = 1, r = p;
			while(l != r)
			{
				int mid = (l + r) / 2;
				if(s(mid) < p && s(l)< p)
				{
					l = mid;
				}
				else if(s(mid) > p, s(r)> p)
				{
					r = mid;
				}
				if(r - l == 1)
					break;
			}
			printf("Case %d: ", j);

			if(s(l) == p)
				printf("%d\n", l*5);
			else if(s(r) == p)
				printf("%d\n", r*5);
			else
				printf("I need anwei\n");
		}
	}
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值