定价

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/Tan_tan_tann/article/details/102669856

定价

题解

此题就是一个模拟题。你只需要从l开始枚举末尾为5与0的数,记录最小值即可。

源码

#include<cstdio>
#include<cmath>
#include<iostream> 
using namespace std;
typedef long long LL;
const LL INF=0x3f3f3f3f;
LL t,l,r;
/*#define gc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<22,stdin),p1 == p2)?EOF:*p1++)
char buf[(1<<22)],*p1=buf,*p2=buf;*/
#define gc() getchar()
template<typename _T>
inline void read(_T &x)
{
	int f=1;x=0;char s=gc();
	while(s>'9'||s<'0'){if(s=='-')f=-1;s=gc();}
	while(s>='0'&&s<='9'){x=(x<<3)+(x<<1)+(s^48);s=gc();}
	x*=f;
}
LL score(LL x)
{
	LL res=0,b=x;
	while(b) b/=10,res+=2;
	if(x%10==5) res--;
	return res;
}//计算分值
int main()
{
	read(t);
	while(t--)
	{
		read(l);read(r);
		LL a=l,k=1,minn=score(a),ans=a;
		while(a)
		{
			LL b=a+(5-a%10);//枚举末尾为5
			if(b*k<=r&&b*k>=l)
				if(score(b)<minn) minn=score(b),ans=b*k;
			if(a%10!=0)b=a+(10-a%10);//枚举末尾为0
			b/=10;k*=10;if(b*k>r) break;//更换下一个值
			if(score(b)<minn) minn=score(b),ans=b*k;
			a=b;
		}
		printf("%lld\n",ans);
	}
	return 0;
}

谢谢!!!

猜你喜欢

转载自blog.csdn.net/Tan_tan_tann/article/details/102669856