1

每个人都知道如何将数字从十进制系统转换为二进制。我也是。每个人也都知道如何从十进制转换为三进制的基数。

但是,我有一个问题,我需要将十进制数转换为“奇怪的以 3 为底的”系统,其中一个符号不能是第一个符号,而应该被其余​​两个符号包围。因此,在使用其他两个符号之一之前,不能重复一个符号。

因此,如果“0”是不能是第一个且不能重复的符号:

完全合法的数字:120、110202、1020

不应该存在的数字:01212(零不能在前面),120012(零不能重复)

请有人帮忙提出一种算法,将十进制系统转换为这个“奇怪的基数 3”系统并返回。

先感谢您

4

2 回答 2

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
于 2010-06-30T12:32:05.967 回答
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 -> 121 y = 2 // {0, 100}

16+2 -> 200 y = 3 // {0, 100, 200}

16+3-2 -> 201 年 = 3

另一种方式:

y:= IllRep(x);     // calculate the number y of illegal representations
z:= ThreeToDec(x); // convert x from 3-based to dec 
result:= z-y;      

现在您所需要的只是一个查找所有非法表示的函数,直到达到一定数量。

于 2010-06-30T13:49:56.343 回答