【发布时间】:2013-03-02 10:36:56
【问题描述】:
我有一个“n”位号码。我需要打印相同的前 n - 1 位数字。据我所知,它基本上是除以 10。但这是在不使用任何算术运算符的情况下完成的。我尝试使用 ctype 中的一些转换,但由于数字的确切长度,即没有。位数未知,我无法使用。
即
convert int to string, say 12345 to string
set the last position of it to '\0', in my case last position is not known
//i.e. 1, 2, 3, 4, '\0', assuming string size is 5
convert it back to int.// 1234
是否有任何调整,或者完全不同的东西?
【问题讨论】:
-
顺便说一句,将 int 转换为字符串将涉及算术 - 好的,所以您可以使用标准函数来完成,但它们将使用算术。
-
如何获得“数字”,是用户输入的,还是您在程序中输入的 int?