zoukankan      html  css  js  c++  java
  • 暑假集训 #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),那么考虑一下循环性质,找规律,多写出几个数字,就发现规律了

  • 相关阅读:
    JS正则与PHP正则
    关于微信扫码支付的流程
    Jquery快速入门
    phpstorm快捷键大全
    CentOS 7.3 下部署基于 Node.js的微信小程序商城
    一个故事告诉你比特币的原理及运作机制 (转 2013)
    mysql The used table type doesn’t support FULLTEXT indexes 解决方案 (phpstudy 会出现),coten不会
    linux下使用 du查看某个文件或目录占用磁盘空间的大小
    ◆织梦内容管理系统模板标签代码参考
    Linux 下挂载新硬盘方法(转)
  • 原文地址:https://www.cnblogs.com/smilesundream/p/5667900.html
Copyright © 2011-2022 走看看