我正在尝试将pep8中的数字分解为数千位数字,然后是代表数字数百的部分,然后是表示十位数的部分,最后是代表数字的部分。
例如:数字439应该这样分解,0,4,3,9。
你有什么建议吗?
发布于 2021-10-05 18:16:14
数字439是如何存储的?它是一个字符串,还是存储在二进制文件中的数值?
你有什么约束?如果你知道这个数字总是< 10,000,并且总是非负的,你可以做一些简化的选择。
此外,您还需要考虑是否需要数值0、4、3、9还是字符串形式"0439“。
下面的文章有很多很好的答案,特别是对于没有硬件鸿沟或mod的小型处理器:https://electronics.stackexchange.com/questions/12618/fastest-way-to-get-integer-mod-10-and-integer-divide-10
有几种方法:
的东西
- similar to the above but with substitutions for division or mod by 10 using subtraction, shifting, and other; this answer shows a simple way to extract digits from non-negative number using subtraction instead of division [https://electronics.stackexchange.com/a/91940](https://electronics.stackexchange.com/a/91940)二进制数字到BCD的
https://electronics.stackexchange.com/a/112455
您还可以搜索名为itoa的算法--它表示整数到ascii。如果您有一个单独的十进制数字隔离(如上面的一些数字),则可以将其转换为ascii字符,例如,添加48,这是“0”的ascii代码--数字0的可打印字符。这是b/c,十进制数字的字符代码是连续的,所以9+ 48 = 57,即'9‘。
https://stackoverflow.com/questions/69453400
复制相似问题