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

本文介绍了一个通过双倍及数字排序操作生成特定整数序列的程序设计问题。该序列从1开始,后续项通过将前一项翻倍并按升序排列其十进制数字得到。文章提供了一段C++代码实现,用于计算序列中指定位置的数值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值