将小数转换为只有两位数的二进制数:1 和 2
Converting decimals into binaries of only two digits: 1 and 2
我的大学课本上的练习有问题。这是:
We are interested in a binary system representing positive integers with only two digits: 1 and 2 (no zeroes!). The subsequent positions correspond to the successive powers of the two, as in the usual binary notation: at the k-th position there is a digit whose value is multiplied by 2^k for k = 0, 1, 2... . In this system - as there are no leading zeros - in addition to formerly mentioned number representation, we use a value that specifies the number of selected digits, let's call it c. Each number is therefore represented by a pair (a, c), where a is a finite sequence of 1 and 2, and c determines the length of that sequence. For example, the pair (12, 3) represents the number 4, and the pair (221, 3) represents the number 13. Write a function which, for a positive number x, determines the representation of its value in the system in question and passes it through the parameter y. Let's agree that in case x is not positive, the value of field c should be 0.
我发现我可以轻松地将十进制输入转换为二进制,然后将二进制转换为练习中提到的系统。从右边开始,我需要通过将高位数字 1 转换为低位数字 2 来删除零。
例如:十进制=21。二进制=10101。系统形成练习=10101 -> 10021 ; 10021 -> 02021 -> 01221
但是,可能有更有效的解决方案,可以将练习中的十进制直接转换为系统。感谢您帮助找到算法思维路径。然后我会自己编码以确保我理解它。
这是我第一次 post 上论坛,英语不是我的母语。如果我表达不够清楚,请见谅。
亲切的问候
设 n 为我们要表示的数字。数字c要满足
20 + 21 + … + 2c−1 = 2c − 1 ≤ n ≤ 2c+1 - 2 = 2 (20 + 21 + … + 2c−1).
经查,这些区间划分了自然数,所以唯一解为c = ⌊log2 (n+1)⌋。 floor-log 通常可以用一条指令计算,或者您可以使用 bit-twiddling hack.
一旦我们知道 c,我们只需要找到 n − (20 + 21 + … + 2c−1) = n − (2c − 1) 每个数字加一。
我的大学课本上的练习有问题。这是:
We are interested in a binary system representing positive integers with only two digits: 1 and 2 (no zeroes!). The subsequent positions correspond to the successive powers of the two, as in the usual binary notation: at the k-th position there is a digit whose value is multiplied by 2^k for k = 0, 1, 2... . In this system - as there are no leading zeros - in addition to formerly mentioned number representation, we use a value that specifies the number of selected digits, let's call it c. Each number is therefore represented by a pair (a, c), where a is a finite sequence of 1 and 2, and c determines the length of that sequence. For example, the pair (12, 3) represents the number 4, and the pair (221, 3) represents the number 13. Write a function which, for a positive number x, determines the representation of its value in the system in question and passes it through the parameter y. Let's agree that in case x is not positive, the value of field c should be 0.
我发现我可以轻松地将十进制输入转换为二进制,然后将二进制转换为练习中提到的系统。从右边开始,我需要通过将高位数字 1 转换为低位数字 2 来删除零。
例如:十进制=21。二进制=10101。系统形成练习=10101 -> 10021 ; 10021 -> 02021 -> 01221
但是,可能有更有效的解决方案,可以将练习中的十进制直接转换为系统。感谢您帮助找到算法思维路径。然后我会自己编码以确保我理解它。
这是我第一次 post 上论坛,英语不是我的母语。如果我表达不够清楚,请见谅。
亲切的问候
设 n 为我们要表示的数字。数字c要满足
20 + 21 + … + 2c−1 = 2c − 1 ≤ n ≤ 2c+1 - 2 = 2 (20 + 21 + … + 2c−1).
经查,这些区间划分了自然数,所以唯一解为c = ⌊log2 (n+1)⌋。 floor-log 通常可以用一条指令计算,或者您可以使用 bit-twiddling hack.
一旦我们知道 c,我们只需要找到 n − (20 + 21 + … + 2c−1) = n − (2c − 1) 每个数字加一。