我正在尝试编写一个函数,将十进制转换为平衡七廿五进制(0123456789ABCDEFGHKMNPRTVXZ),其中0表示-13,D:0,Z:13。
我已经尝试过这个函数,但有些情况无法正常工作。
在这个例子中,-14、-15、14和15都不能正常工作。
我已经尝试过这个函数,但有些情况无法正常工作。
static const std::string HEPT_CHARS = "0123456789ABCDEFGHKMNPRTVXZ";
std::string heptEnc(int value){
std::string result = "";
do {
int pos = value % 27;
result = std::string(HEPT_CHARS[(pos + 13)%27] + result);
value = value / 27;
} while (value != 0);
return result;
}
在这个例子中,-14、-15、14和15都不能正常工作。
call(x) - expect: result
heptEnc(-9841) - 000: 000
heptEnc(-15) - CX:
heptEnc(-14) - CZ:
heptEnc(-13) - 0: 0
heptEnc(-1) - C: C
heptEnc(0) - D: D
heptEnc(1) - E: E
heptEnc(13) - Z: Z
heptEnc(14) - E0: 0
heptEnc(15) - E1: 1
heptEnc(9841) - ZZZ: ZZZ
(-15) % 27 == -15
,然后您访问负偏移量的HEPT_CHARS
。 - Igor Tandetnikvalue == 14
,你需要得到2个数字,但是value = value / 27;
这行代码不正确,因为循环在第一个数字后就会停止。 - mch