日期:2014-05-20  浏览次数:20711 次

java求最大公约数,最小公倍数很弱智的问题
这是我在网上看到的代码

import java.util.Scanner;
public class Chpt7_4 {
public static int fun1(int a1,int b1)
 { int c;
  c=a1%b1;
  while (c>0)
  {a1=b1;
  b1=c;
  c=a1%b1;
  return (b1);
  }
  }
 public static void main(String[] args) {
  int a,b,temp;
  int m;
  Scanner in=new Scanner(System.in);
  a=in.nextInt();
  b=in.nextInt();
  if(a<b)
  {temp=a;a=b;b=temp;
  }
  m=fun1(a,b);
  System.out.println("最大公约数为:"+m);
  System.out.println("最小公倍数为:"+(a*b)/m);
  }
我一直觉得temp的定义,大小的置换很多余,你们觉得啦

------解决方案--------------------
同意,上面的方法里已经有了判断大小的功能了
------解决方案--------------------
嗯。可以:
a=a+b;
b=a-b;
a=a-b;
这样就可以交换了