It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center。
6年前关闭。
我有一个“ n”位数。我需要打印相同的前n-1位数字。据我所知,它基本上是除以10的。但这要在不使用任何算术运算符的情况下完成。我尝试在ctype中使用某些转换,但由于数字的确切长度,即没有。的位数未知,我无法使用相同的数字。
即
是否有任何调整或其他完全不同的选择?
6年前关闭。
我有一个“ 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
是否有任何调整或其他完全不同的选择?
最佳答案
使用sprintf()
将int转换为字符串。您需要char buffer[SOMESIZE];
才能传递到sprintf
。现在您有了一个可以操作的字符串。 strlen()
将为您提供长度[1]。其余应遵循您概述的内容。
我会让您确切地知道如何将这些位放在一起。
[1]实际上,您可以使用sprintf
的返回值作为长度,因为它返回字符串中产生的字符数。
关于c - n-1个数字的数字,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15173484/