Java 生成序列/返回序列中的第n个项

Java 生成序列/返回序列中的第n个项,java,algorithm,data-structures,java-8,sequence,Java,Algorithm,Data Structures,Java 8,Sequence,我需要生成一个序列,使其成员只包含1,2,3位。例如,12311121321231333111….等等,直到10^18术语。 我无法推断出这方面的任何模式。似乎不可能在系列中编写最多10^18个术语的代码 1、2、3、11、12、13、21、22、23、31、32、33、111、112、113、121、122、, 123, 131, 132, 133, 211, 212, 213, 221, 222, 223, 231, 232, 233, 311, 312, 313, 321, 322, 32

我需要生成一个序列,使其成员只包含
1
2
3
位。例如,
12311121321231333111….
等等,直到
10^18
术语。 我无法推断出这方面的任何模式。似乎不可能在系列中编写最多10^18个术语的代码

1、2、3、11、12、13、21、22、23、31、32、33、111、112、113、121、122、, 123, 131, 132, 133, 211, 212, 213, 221, 222, 223, 231, 232, 233, 311, 312, 313, 321, 322, 323, 331, 332, 333, 1111, 1112, 1113, 1121, 1122, 112311311321133211211211211212121212122121


我希望在序列中找到给定的第n项。这是一种数字系统,它只包含
1
2
3
,或者是这些数字的组合,就像我们通常的数字系统一样,按顺序解释为一个数字。

这只是一种基数为3的数字系统,只是数字从1到3,而不是从0到2。数学的计算方法是一样的:

1=1*3^0
2=2*3^0
3=3*3^0
4=1*3^1+1*3^0
5=1*3^1+2*3^0
6=1*3^1+3*3^0
7=2*3^1+1*3^0

19=1*3^2+3*3^1+1*3^0

写两种方法:

  • 数字(n):为给定的
    n
    计算最右边的数字。一些测试用例:数字(4)=1,数字(5)=2,数字(15)=3
  • 剩余(n):计算表示n但最右边数字被截断的数字。一些测试用例:遗留(4)=1,遗留(15)=4,遗留(23)=7

  • 现在,将这两种方法结合到问题的解决方案中,反复切掉最右边的数字,直到一无所有。您可能会发现递归执行此操作更容易。

    您已经提到的序列称为。它是由

    您可以在上面的链接中查看公式的解释。到目前为止,我已经使用
    long
    编写了它的基本级别。要达到
    10^18
    术语,您需要使用Java类

    class SequenceGeneratorWith123 {
    
        // Written by Soner
    
        private static double logOfBase(long base, long num) {
            return Math.log(num) / Math.log(base);
        }
    
        private static int mfunc(long n) {
            return (int) Math.floor(logOfBase(3, 2 * n + 1));
        }
    
        private static int b(int j, double m, long n) {
            return (int) Math.floor((2 * n + 1 - Math.pow(3, m)) / (2 * Math.pow(3, j)));
        }
    
        public static void main(String[] args) {
    
            for (int i = 0; i < 9; i++) {
                long n = (long) Math.pow(10, i);
                int m = mfunc(n);
                long sum = 0;
    
                for (int j = 0; j < m ; j++) {
                    sum += ((1 + b(j, m, n) % 3) * Math.pow(10, j));
                }
                System.out.printf("a(10^%d) = %d\n", i, sum);
            }
    
            System.out.println("After the point, overflow will occur " +
                            "because of long type.");    
        }
    }
    

    您只需要玩一下代码,也就是说,我们只需稍微更改一下
    main()
    ,就可以实现您的愿望

    long n = 1;
    // How many terms you need you can alter it by pow() method.
    // In this example 10^2 = 100 terms will be obtained.
    int term = (int)Math.pow(10, 2);
    for (int i = 0; i < term; i++) {
    
        int m = mfunc(n);
        long sum = 0;
    
        for (int j = 0; j < m ; j++) {
            sum += ((1 + b(j, m, n) % 3) * Math.pow(10, j));
        }
        System.out.printf("%d. term = %d\n", i + 1, sum);
        n++;
    }
    

    序列中有多少个长度为k的项目?由此,您可以编写一个有效的程序来确定序列中第n项的长度吗?是的,对于同一件事,我推导了这么多术语,但在这之后,我感到困惑。你能为我简化一下吗?因为我是编程新手,所以N最多可以是10^18个项,所以我认为如果我们在输入的同时计算它,在竞争性编程中可能不会那么有效。
    a(10^0) = 1
    a(10^1) = 31
    a(10^2) = 3131
    a(10^3) = 323231
    a(10^4) = 111123331
    a(10^5) = 11231311131
    a(10^6) = 1212133131231
    a(10^7) = 123133223331331
    a(10^8) = 13221311111312132
    After the point, overflow will occur because of long type.
    
    long n = 1;
    // How many terms you need you can alter it by pow() method.
    // In this example 10^2 = 100 terms will be obtained.
    int term = (int)Math.pow(10, 2);
    for (int i = 0; i < term; i++) {
    
        int m = mfunc(n);
        long sum = 0;
    
        for (int j = 0; j < m ; j++) {
            sum += ((1 + b(j, m, n) % 3) * Math.pow(10, j));
        }
        System.out.printf("%d. term = %d\n", i + 1, sum);
        n++;
    }
    
    1. term = 1
    2. term = 2
    3. term = 3
    4. term = 11
    5. term = 12
    6. term = 13
    7. term = 21
    8. term = 22
    9. term = 23
    10. term = 31
    11. term = 32
    12. term = 33
    13. term = 111
    14. term = 112
    15. term = 113
    16. term = 121
    17. term = 122
    18. term = 123
    19. term = 131
    20. term = 132
    21. term = 133
    22. term = 211
    23. term = 212
    24. term = 213
    25. term = 221
    26. term = 222
    27. term = 223
    28. term = 231
    29. term = 232
    30. term = 233
    31. term = 311
    32. term = 312
    33. term = 313
    34. term = 321
    35. term = 322
    36. term = 323
    37. term = 331
    38. term = 332
    39. term = 333
    40. term = 1111
    41. term = 1112
    42. term = 1113
    43. term = 1121
    44. term = 1122
    45. term = 1123
    46. term = 1131
    47. term = 1132
    48. term = 1133
    49. term = 1211
    50. term = 1212
    51. term = 1213
    52. term = 1221
    53. term = 1222
    54. term = 1223
    55. term = 1231
    56. term = 1232
    57. term = 1233
    58. term = 1311
    59. term = 1312
    60. term = 1313
    61. term = 1321
    62. term = 1322
    63. term = 1323
    64. term = 1331
    65. term = 1332
    66. term = 1333
    67. term = 2111
    68. term = 2112
    69. term = 2113
    70. term = 2121
    71. term = 2122
    72. term = 2123
    73. term = 2131
    74. term = 2132
    75. term = 2133
    76. term = 2211
    77. term = 2212
    78. term = 2213
    79. term = 2221
    80. term = 2222
    81. term = 2223
    82. term = 2231
    83. term = 2232
    84. term = 2233
    85. term = 2311
    86. term = 2312
    87. term = 2313
    88. term = 2321
    89. term = 2322
    90. term = 2323
    91. term = 2331
    92. term = 2332
    93. term = 2333
    94. term = 3111
    95. term = 3112
    96. term = 3113
    97. term = 3121
    98. term = 3122
    99. term = 3123
    100. term = 3131