解决这个序列的逻辑是什么?

发布于 2024-09-18 03:13:49 字数 1459 浏览 5 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

油饼 2024-09-25 03:13:49

二进制,从 2 开始计数,忽略前导数字,使用 7 和 8 表示 0 和 1:

        7,  8,  77,  78,  87,  88,  777,  778,  787,  788
 0, 1, 10, 11, 100, 101, 110, 111, 1000, 1001, 1010, 1011 

Binary, counting from two, ignoring the leading digit, using 7 and 8 for zero and one:

        7,  8,  77,  78,  87,  88,  777,  778,  787,  788
 0, 1, 10, 11, 100, 101, 110, 111, 1000, 1001, 1010, 1011 
若水般的淡然安静女子 2024-09-25 03:13:49

观察:

  1. 该序列似乎是仅包含数字 7 和 8 的数字升序列表。

  2. 位数是非递减的,对于每个 n 位数字部分,序列中有 2 ** n 个数字。

  3. n位数字前半部分以7开头,后半部分以8开头。

  4. < p>对于n位数字的每一半,第一个之后的剩余数字与n-1位数字相同。

这些事实可用于构建相当有效的递归实现。

这是一个 C# 实现:

void Main() {
    for (int i = 0; i < 10; i++)
        Console.WriteLine (GetSequence(i));
}

string GetSequence(int idx) {
    if (idx == 0) return "7";
    if (idx == 1) return "8";

    return GetSequence(idx / 2 - 1) + GetSequence(idx % 2);
}

输出:

7
8
77
78
87
88
777
778
787
788

Observations:

  1. The sequence appears to be an ascending list of numbers containing only the digits 7 and 8.

  2. The number of digits is non-decreasing and for each n-digit section, there are 2 ** n numbers in the sequence.

  3. The first half of the n-digit numbers starts with 7, and the second half starts with 8.

  4. For each half of the n-digit numbers, the remaining digits after the first are the same as the n-1 digit numbers.

These facts can be used to construct a reasonably efficient recursive implementation.

Here is a C# implementation:

void Main() {
    for (int i = 0; i < 10; i++)
        Console.WriteLine (GetSequence(i));
}

string GetSequence(int idx) {
    if (idx == 0) return "7";
    if (idx == 1) return "8";

    return GetSequence(idx / 2 - 1) + GetSequence(idx % 2);
}

Output:

7
8
77
78
87
88
777
778
787
788
嘴硬脾气大 2024-09-25 03:13:49

由于块的大小呈指数增长(2 个长度为 1 的元素、4 个长度为 2 的元素、8 个长度为 3 的元素等),因此您可以轻松确定结果编号中的位数。

    long block_size = 2;
    int len = 1;
    while (n > block_size) {
        n -= block_size;  // n is changed here
        block_size *= 2;
        ++len;
    }

现在,您只需创建 n - 1 的二进制表示形式,其中 7 表示零,8 表示一(用零将其填充到长度 len)。很简单。

我假设索引从 1 开始。

Since size of block is growing exponentially (2 elements of length 1, 4 elements of length 2, 8 elements of length 3, etc), you can easily determine number of digits in result number.

    long block_size = 2;
    int len = 1;
    while (n > block_size) {
        n -= block_size;  // n is changed here
        block_size *= 2;
        ++len;
    }

Now, you just create binary representation of n - 1, with 7 for zeroes and 8 for ones (padding it to length len with zeroes). Quite simple.

I assume indexes start from 1 here.

我一直都在从未离去 2024-09-25 03:13:49

将 7 替换为 0,将 8 替换为 1,并将其视为二进制序列

substitute 0 for 7 and 1 for 8 and treat it like a binary sequence

你的心境我的脸 2024-09-25 03:13:49

编写为 PHP。我假设序列元素从 1 开始编号。

$n = 45;
// let's find the 45th sequence element.
$length = 1;
while ( $n >= pow(2, $length + 1) - 1 ) {
    $length++;
}
// determine the length in digits of the sequence element
$offset = $n - pow(2, $length) + 1;
// determine how far this sequence element is past the
// first sequence element of this length
$binary = decbin($offset);
// obtain the binary representation of $offset, as a string of 0s and 1s
while ( strlen($binary) < $length ) {
    $binary = '0'.$binary;
}
// left-pad the string with 0s until it is the required length
$answer = str_replace( array('0', '1'),
                       array('7', '8'),
                       $binary
                       );

Written as PHP. I assume that the sequence elements are numbered starting from 1.

$n = 45;
// let's find the 45th sequence element.
$length = 1;
while ( $n >= pow(2, $length + 1) - 1 ) {
    $length++;
}
// determine the length in digits of the sequence element
$offset = $n - pow(2, $length) + 1;
// determine how far this sequence element is past the
// first sequence element of this length
$binary = decbin($offset);
// obtain the binary representation of $offset, as a string of 0s and 1s
while ( strlen($binary) < $length ) {
    $binary = '0'.$binary;
}
// left-pad the string with 0s until it is the required length
$answer = str_replace( array('0', '1'),
                       array('7', '8'),
                       $binary
                       );
一身软味 2024-09-25 03:13:49

它看起来像一个简单的二进制序列,其中7代表二进制0,8代表二进制1。

It looks like a simple binary sequence, where 7 represents binary zero, and 8 represents binary 1.

情愿 2024-09-25 03:13:49

您可以通过执行以下操作,直接计算第 N 个数字 (num),无需递归或循环(示例代码位于 MATLAB 中):

  • 计算数字中的位数:

    nDigits = 楼层(log2(num+1));
    
  • 查找数字 num 的二进制表示形式(仅前 nDigits 位)首先减去 2 的 nDigits 次方:

    binNum = dec2bin(num-(2^nDigits-1),nDigits);
    
  • 将 7 添加到 1 和 0 字符串中的每个值:

    结果 = char(binNum+7);
    

这是一个测试,将上述三个步骤放入一个匿名函数 f 中:

>> f = @(n) char(dec2bin(n+1-2^floor(log2(n+1)),floor(log2(n+1)))+7);
>> for n = 1:20, disp(f(n)); end
7
8
77
78
87
88
777
778
787
788
877
878
887
888
7777
7778
7787
7788
7877
7878

You can compute this directly for the Nth number (num) without recursion or looping by doing the following (the sample code is in MATLAB):

  • Compute the number of digits in the number:

    nDigits = floor(log2(num+1));
    
  • Find the binary representation of the number num (only the first nDigits digits) after first subtracting one less than two raised to the power nDigits:

    binNum = dec2bin(num-(2^nDigits-1),nDigits);
    
  • Add 7 to each value in the string of ones and zeroes:

    result = char(binNum+7);
    

And here's a test, putting the above three steps into one anonymous function f:

>> f = @(n) char(dec2bin(n+1-2^floor(log2(n+1)),floor(log2(n+1)))+7);
>> for n = 1:20, disp(f(n)); end
7
8
77
78
87
88
777
778
787
788
877
878
887
888
7777
7778
7787
7788
7877
7878
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文