本帖最后由 2528870651 于 2014-4-3 14:40 编辑
- <div class="blockcode"><blockquote>最大公约数:
- public class CommonDivisor{
- public static void main(String args[])
- {
- commonDivisor(24,32);
- }
- static int commonDivisor(int M, int N)
- {
- if(N<0||M<0)
- {
- System.out.println("ERROR!");
- return -1;
- }
- if(N==0)
- {
- System.out.println("the biggest common divisor is :"+M);
- return M;
- }
- return commonDivisor(N,M%N);
- }
- }
- 最小公倍数和最大公约数:
- public class CandC
- {
- //下面的方法是求出最大公约数
- public static int gcd(int m, int n)
- {
- while (true)
- {
- if ((m = m % n) == 0)
- return n;
- if ((n = n % m) == 0)
- return m;
- }
- }
- public static void main(String args[]) throws Exception
- {
- int a=3; int b=2;
- int c = gcd(a, b);
- System.out.println("最小公倍数:" + a * b / c + "\n最大公约数:" + c);
- }
- }
复制代码
可以吗??? |