这个真是怎么贪心都能过了(然而我太弱了还是挂了好几遍)
#include<bits/stdc++.h>
using namespace std;
int L,R,T,mn,ans;
inline int read()
{
int a=0,f=1; char c=getchar();
while (c<'0'||c>'9') {if (c=='-') f=-1; c=getchar();}
while (c>='0'&&c<='9') {a=a*10+c-'0'; c=getchar();}
return a*f;
}
int add(int x)
{
int k=1;
while (x%10==0) k*=10,x/=10;
return k;
}
int calc(int x)
{
while (x%10==0) x/=10;
int t=x%10,a=0;
while (x) x/=10,a++;
if (t==5) return 2*a-1; return 2*a;
}
int main()
{
T=read();
while (T--)
{
L=read(),R=read();
mn=calc(L); ans=L;
while (1)
{
L+=add(L);
if (L>R) break;
int t=calc(L);
if (t<mn) mn=t,ans=L;
}
printf("%d\n",ans);
}
return 0;
}
本文介绍了一个使用贪心策略解决特定数学问题的C++程序。该程序通过不断优化选择过程来寻找区间[L,R]内满足特定条件的最小整数。通过对输入范围内的每个数进行特殊处理,最终找到最优解。
685

被折叠的 条评论
为什么被折叠?



