两个非负整数相乘的递归方法
刚刚在过去的考试试卷中看到了这一点,我正在寻找最好的方法,因为我无法弄清楚。我们的答案中不允许使用乘法,必须使用重复的加法。它也必须是递归的而不是迭代的。
public static int add(int a, int b) {
}
谁能帮我解决这个问题吗? 多谢。
Just saw this in a past exam paper and am looking for the best way to do it since I can't figure it out. We are not allowed use multiplication in our answer, it must use repeated addition. It must also be recursive and not iterative.
public static int add(int a, int b) {
}
Can anyone help me figure this out please?
Thanks a lot.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果允许轮班和改装,这里有一个有趣的。
或(用 & 代替 mod):
If shifts and mods are allowed, here's a fun one.
or (with an & instead of mod):
cidermonkey 编写的是古埃及乘法的实现,并且至少在 3700 年前就已被使用。
例如,要乘以 27 * 37,请写入两个数字,然后重复将第一个数字减半并将第二个数字加倍:
然后,将第二列中第一列中具有奇数的数字相加:
该方法今天仍然有效。数学就是这样好。
What cidermonkey has written is an implementation of ancient Egyptian multiplication, and was used at least 3700 years ago.
For example, to multiply 27 * 37, write the two numbers and repeated halve the first number and double the second:
then, add the numbers in the second column which have a odd number in the first column:
The method still works today... mathematics is good like that.