sequence 这个算……找规律的题吧

来源:互联网 发布:免费广告配音软件 编辑:程序博客网 时间:2024/04/29 03:14

Problem description

Integer sequences are very interesting mathematical objects. Let us examine a sequence generated with the use of two operations: doubling and “digit sorting”. The latter operation consists in ascending-order sort of the individual digits in the decimal representation of the argument. For example, “digit sorting” of number 5726 gives 2567.
The first member of the considered sequence is 1. To generate a member of the sequence from the previous member, double the previous one and apply “digit sorting” to the result. The first 15 members of the sequence are as follows: 1, 2, 4, 8, 16, 23, 46, 29, 58, 116, 223, 446, 289, 578, 1156, …
Write a program to determine the value of the n-th member of this sequence.


Input

The first line contains an integer n, the number of sequence member to be calculated. (1<=n<= 2 147 483 647)


Output

The output file should contain a single integer k, the value of the n-th member of the sequence.


Sample Input
16
Sample Output
123

是个水题,但是刚开始的时候完全没有发现到后面的数是一样的,所以纠结了很久……罚时很高的说……。所以碰到范围特别大的题,很可能存在小tipppp。然后就是在处理数字和字符的时候会有一点小技术吧。

#include <stdio.h>#include <iostream>#include <algorithm>#define maxn 2300using namespace std;__int64 n;char s[maxn];int a[27];int b[6]={155578,111356,122227,244445,48889,77789};int comp(char a,char b){    return a>b;}void go(){    int i,j,k;    int p,q,r,x,y;    a[1]=1;    for(i=2;i<=26;i++)    {        a[i]=a[i-1]*2;        p=a[i];        q=p%10;        r=p/10;        x=0;        while(q||r)        {            s[x++]=q+'0';            q=r%10;            r=r/10;        }        sort(s,s+x,comp);        s[x]='\0';        a[i]=0;        while(x--)        {            a[i]=a[i]*10+s[x]-'0';        }    }}int main(){    go();    while(scanf("%d",&n)!=EOF)    {        if(n<=26 ) printf("%d\n",a[n]);        else        {            printf("%d\n",b[(n-27)%6]);        }    }    return 0;}


0 0