每个人都知道如何将数字从十进制转换为二进制。我也这样做。每个人都知道如何从十进制转换为三个系统的基础。
但是,我有一个问题,我需要将十进制数转换为“奇怪的基数3”系统,其中一个符号不能是第一个,应该被其余的两个包围。因此,在使用其他两个符号之一之前,不能重复一个符号。
因此,如果“0”是不能成为第一个且不能重复的符号:
完全合法的数字:120,110202,1020
不应存在的数字: 01212(零不应在前面),120012(零不能重复)
请有人帮助提出一种算法,该算法可以从十进制系统转换为这个“奇怪的基础3”系统并返回。
提前谢谢
答案 0 :(得分:0)
以下是所需的映射吗?
0 <- illegal
1 0
2 1
10 2
11 3
12 4
20 5
21 6
22 7
100 <- illegal
101 8
102 9
110 10
111 11
112 12
120 13
121 14
122 15
200 <- illegal
201 16
202 17
210 18
211 19
212 20
220 21
221 22
222 23
1000 <- illegal
1001 <- illegal
1002 <- illegal
1010 24
1011 25
1012 26
1020 27
1021 28
1022 29
1100 <- illegal
1101 30
1102 31
1110 32
1111 33
1112 34
1120 35
1121 36
1122 37
1200 <- illegal
1201 38
1202 39
1210 40
1211 41
1212 42
1220 43
1221 44
1222 45
2000 <- illegal
答案 1 :(得分:0)
基于@Daniel的映射,从Dec到基于奇怪的3:
x := n; // Original number
y:= 0;
do
y0:= y;
z:= DecToThree(x); // Convert x from Decimal to 3-based.
y:= IllRep(z); // Calculate the number y of numbers with at least 2
// consecutive 0 with a representation in 3- based.
x:= n + y; // Add illegal representations to original number;
until (y = y0);
Result:= DezToThree(x); // Convert x from Decimal to 3-based.
示例:
16 - &gt; 121 y = 2 // {0,100}
16 + 2 - &gt; 200 y = 3 // {0,100,200}
16 + 3-2 - &gt; 201 y = 3
相反:
y:= IllRep(x); // calculate the number y of illegal representations
z:= ThreeToDec(x); // convert x from 3-based to dec
result:= z-y;
现在你所需要的只是一个找到所有非法表征的函数。