• 暑假集训 #3div2 C Sequence 数字找规律


    C. Sequence (64 Mb, 1 sec / test)
    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.
    Limitations 1 ≤ n ≤ 2 147 483 647. Input The first line contains an integer n, the number of sequence member to be calculated. Output The output file should contain a single integer k, the value of the n-th member of the sequence. Example

    Input.txt Output.txt 1 1
    Input.txt Output.txt 6 23

    题意:1, 2, 4, 8, 16, 23, 46, 29, 58, 116, 223, 446, 289, 578, 1156.....这样的数字,每一个数字

    是由前一个数字乘以2,然后将每位数字由小到大排序得到的,问第n个数字是多少,n<=1e9;

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <queue>
    #include <cmath>
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <queue>
    #include <cmath>
    using namespace std;
    
    int a[500]={
     0,1,2,4,8,16,23,46,29,58,116,223,446,289,578,1156,1223,2446,2489,
     4789,5789,11578,12356,12247,24449,
     48889,77789,155578,111356,122227,244445
    };
    int main()
    {
        freopen("input.txt","r",stdin);
        freopen("output.txt","w",stdout);//文件输入
        int n;
        while(~scanf("%d",&n))
        {
            if(n<=30) printf("%d
    ",a[n]);
            else {
                    n-=24;
                   if(n%6==0) printf("%d
    ",a[30]);
                   else printf("%d
    ",a[n%6+24]);
            };
        }
        fclose(stdin);
        fclose(stdout);
        return 0;
    }
    

     分析:这题还是很好的,首先看到n<=1e9,这么大的算法,打表O(n)什么的是肯定不行了,那么考虑一下直接构造,会发现

    比较复杂,而且还得借助前一个数字,复杂度又是O(n),那么考虑一下循环性质,找规律,多写出几个数字,就发现规律了

  • 相关阅读:
    Asp.net性能优化总结(一)
    Visual C#常用函数和方法集汇总
    ASP.net下大文件上传的解决方案及WebbUpload组件源码
    正则表达式学习
    在Asp.net中为图像加入版权信息
    Cognos 维度函数
    Cognos8.3函数使用手册(二)
    cognos更新步聚
    Cognos8.3函数使用手册(一)
    Cognos 8 报表备份和恢复
  • 原文地址:https://www.cnblogs.com/smilesundream/p/5667900.html
Copyright © 2020-2023  润新知