思维,暴力,打表

题意:十进制下,每相邻两个数字之间的差值不超过1,叫lunlun数,

给定一个k,让找第k小的lunlun数;

暴力打表找规律;

1,2,3,4,5,6,7,8,9,

10,11,12,

21,22,23,

32,33,34,

43,44,45,

54,55,56,

65,66,67,

76,77,78,

87,88,89,

98,99,

100,101,

110,111,112,

121,122,123,

210,211,212,

221,222,223,

232,233,234,

321,322,323,

332,333,334,

343,344,345,

432,433,434,

443,444,445,

454,455,456,

543,544,545,

554,555,556,

565,566,567,

654,655,656,

665,666,667,

676,677,678,

765,766,767,

776,777,778,

787,788,789,

876,877,878,

887,888,889,

898,899,

987,988,989,

998,999,

1000,1001,

1010,1011,1012,

1100,1101,

1110,1111,1112,

1121,1122,1123,

1210,1211,1212,

1221,1222,1223,

1232,1233,1234,

2100,2101,

2110,2111,2112,

2121,2122,2123,

2210,2211,2212,

2221,2222,2223,

2232,2233,2234,

2321,2322,2323,

2332,2333,2334,

2343,2344,2345,

3210,3211,3212,

3221,3222,3223,

3232,3233,3234,

3321,3322,3323,

3332,3333,3334,

3343,3344,3345,

3432,3433,3434,

3443,3444,3445,

3454,3455,3456,

4321,4322,4323,

4332,4333,4334,

4343,4344,4345,

4432,4433,4434,

4443,4444,4445,

4454,4455,4456,

4543,4544,4545,

4554,4555,4556,

4565,4566,4567,

5432,5433,5434,

5443,5444,5445,

5454,5455,5456,

5543,5544,5545,

5554,5555,5556,

5565,5566,5567,

5654,5655,5656,

5665,5666,5667,

5676,5677,5678,

6543,6544,6545,

6554,6555,6556,

6565,6566,6567,

6654,6655,6656,

6665,6666,6667,

6676,6677,6678,

6765,6766,6767,

6776,6777,6778,

6787,6788,6789,

7654,7655,7656,

7665,7666,7667,

7676,7677,7678,

7765,7766,7767,

7776,7777,7778,

7787,7788,7789,

7876,7877,7878,

7887,7888,7889,

7898,7899,

8765,8766,8767,

8776,8777,8778,

8787,8788,8789,

8876,8877,8878,

8887,8888,8889,

8898,8899,

8987,8988,8989,

8998,8999,

9876,9877,9878,

9887,9888,9889,

9898,9899,

9987,9988,9989,

9998,9999,

10000,10001,

10010,10011,10012,

10100,10101,

10110,10111,10112,

10121,10122,10123,

11000,11001,

11010,11011,11012,

11100,11101,

11110,11111,11112,

11121,11122,11123,

11210,11211,11212,

11221,11222,11223,

11232,11233,11234,

12100,12101,

12110,12111,12112,

........

如上表,可以发现;

末位的前一位为9,两个数字98 99

末位前一位是10,两个数字100 101

其他就三个数字;

找规律好之后,开一个数组,不断计算出第k个数字即可;

code:

#include<iostream>
#include<stdio.h>
using namespace std;
typedef long long ll;
const int maxn=1e5+10;

/*int a[15];
int check(ll x){
    int i,top=0;
    while(x){
        a[++top]=x%10;
        x/=10;
    }
    for(i=2;i<=top;i++)
        if(abs(a[i]-a[i-1])>1)return 0;
    return 1;
}
int main(){
    int cnt=0;
    ll i,n;
    scanf("%lld",&n);
    for(i=1;i<=n;i++)
        if(check(i))
         cnt++,printf("%lld,",i);
    printf("cnt= %d\n",cnt);
    system("pause");
}*/
ll num[maxn]={1,2,3,4,5,6,7,8,9};
int main()
{
    int k;
    scanf("%d",&k);
    if(k<=12)
    {
        printf("%d\n",k);
        //system("pause");
        return 0;
    }
    int cnt=0;
    for(int i=9; i<k; )
    {
        if(!(num[cnt]%10))
        {
            num[i]=num[cnt]*10;
            num[i+1]=num[cnt]*10+1;
            i+=2;
        }
        else if(num[cnt]%10==9)
        {
            num[i]=num[cnt]*10+8;
            num[i+1]=num[cnt]*10+9;
            i+=2;
        }
        else
        {
            num[i]=num[cnt]*10+num[cnt]%10-1;
            num[i+1]=num[cnt]*10+num[cnt]%10;
            num[i+2]=num[cnt]*10+num[cnt]%10+1;
            i+=3;
        }
        cnt++;
    }
    printf("%lld\n",num[k-1]);
    //system("pause");
    return 0;
}

 

 

posted on 2020-04-18 18:06  mmn  阅读(493)  评论(0编辑  收藏  举报