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

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
1
6
Sample Output
1
23

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

#include <stdio.h>
#include <iostream>
#include <algorithm>
#define maxn 2300
using 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值