【发布时间】:2013-08-05 21:31:09
【问题描述】:
如何用 Java 编写这段代码?
def gcd(a, b):
"""
Calculate the Greatest Common Divisor of a and b.
Unless b==0, the result will have the same sign as b (so that when
b is divided by it, the result comes out positive).
"""
while b:
a, b = b, a%b
return a
由于Type mismatch 错误,我似乎无法在Java 中执行while (b) {。看来我也不能完全在 Java 中完成 a, b = b, a%b 行。
【问题讨论】:
-
为什么不买一本关于 Java 的书。这些是基础知识,您将在任何一本书的前几章中学习。
-
@RohitJain 有什么推荐的书吗?
-
@user1757703 docs.oracle.com/javase/tutorial
-
市场上有这么多。您可以从 Bruce Eckel 的 - Thinking in Java 开始。或者试试google.com
-
while b->while (b != 0)。a,b = b, a%b->tmp = b; b=a%b;a=tmp.
标签: java python type-conversion greatest-common-divisor