BZOJ4029: [HEOI2015]定价

【传送门:BZOJ4029


简要题意:

  给出一个定义:每个数都有一个荒谬度,荒谬度是这样计算的:

  1.先将这个数末尾的0去掉

  2.然后设这个数当前的位数为a,如果当前末尾为5,则荒谬度为2a-1,如果不是,则为2a

  给出l,r,求出l到r中的数字中荒谬度最小的数字


题解:

  纯模拟,细节大水题(以后再也不做这种纯细节的题了)


参考代码:

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std;
int w[11];
int L[11],R[11];
int main()
{
    w[1]=1;for(int i=2;i<=10;i++) w[i]=w[i-1]*10;
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int l,r;
        scanf("%d%d",&l,&r);
        if(l==r){printf("%d\n",l);continue;}
        int lenl=0,lenr=0,t;
        memset(L,0,sizeof(L));
        memset(R,0,sizeof(R));
        t=l;while(t!=0) L[++lenl]=t%10,t/=10;
        t=r;while(t!=0) R[++lenr]=t%10,t/=10;
        if(lenl<lenr)
        {
            if(l<=w[lenl]*5) printf("%d\n",w[lenl]*5);
            else if(w[lenl+1]*5<=r) printf("%d\n",w[lenl+1]*5);
            else if(w[lenl]*(L[lenl])==l) printf("%d\n",l);
            else if(w[lenl]*(L[lenl]+1)<=r) printf("%d\n",w[lenl]*(L[lenl]+1));
            else printf("%d\n",w[lenl+1]);
            continue;
        }
        int d=0;
        for(int i=lenl;i>=1;i--) if(L[i]!=R[i]){t=i;break;}else d+=w[i]*L[i];
        if(t!=lenl&&(l==d)) printf("%d\n",l);
        else if(l<=d+w[t]*5&&d+w[t]*5<=r) printf("%d\n",d+w[t]*5);
        else if(d+L[t]*w[t]==l) printf("%d\n",l);
        else printf("%d\n",d+w[t]*(L[t]+1));
    }
    return 0;
}

 

posted @ 2018-03-22 07:48  Star_Feel  阅读(173)  评论(0编辑  收藏  举报