将 int 分成另外 2 个 int
我需要将一个整数分成另外两个整数。第一个 int 不是常量,所以一个问题是,如何处理奇数,因为我只想要整数。例如,如果 int = 5,则 int(2) 将 = 2,int(3) 将 = 3。任何帮助将不胜感激。
I need to divide one int into 2 other int's. the first int is not constant so one problem would be, what to do with odd numbers because I only want whole numbers. For example, if int = 5, then int(2) will = 2 and int(3) will = 3. Any help will greatly be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
假设您想表达
x = a + b
,其中a
和b
与x/2
非常接近尽可能:这是伪代码,您必须从库中找出地板和天花板的实际功能。确保除法以浮点数执行。
作为纯整数:(
对于负数来说这可能有点可疑,因为它取决于负数的除法和取模的行为。)
Supposing you want to express
x = a + b
, wherea
andb
are as close tox/2
as possible:That's pseudo code, you have to find out the actual functions for floor and ceiling from your library. Make sure the division is performed as floating point numbers.
As pure integers:
(This may be a bit fishy for negative numbers, because it'll depend on the behaviour of division and modulo for negative numbers.)
您可以尝试使用数学中的 ceil 和 Floor 函数来生成奇数输入的结果,例如 2 和 3;
You can try ceil and floor functions from math to produce results like 2 and 3 for odd inputs;
好吧,我的答案不是用 Objective-C 写的,但我想你可以很容易地翻译它。
我的想法是:
这样如果起始数字是奇数,第二个数字就会更大。
Well my answer is not in Objective-C but i guess you could translate this easily.
My idea is:
This way the second number will be bigger if the starting number is an odd number.