|
C语言趣味程序百例精解之java实现(42)
最大公约数&最小公倍数程序:public class Test42{
public static void main(String args[]){
int a=Integer.parseInt(args[0]);
int b=Integer.parseInt(args[1]);
int n=gcd(a,b);
System.out.printf("The GCD of %d and %d is: %d\n",a,b,n);
System.out.printf("the LCM of them is:%d\n",a*b/n);
}
public static int gcd(int n,int m){
int temp;
if ( m>n) {
temp = n;
n = m;
m = temp;
}
while(m!=0){
temp=n%m;
n=m;
m=temp;
}
return n;
}
}
C:\java>java Test42 20 55
The GCD of 20 and 55 is: 5
the LCM of them is:220
C:\java>java Test42 17 71
The GCD of 17 and 71 is: 1
the LCM of them is:1207
C:\java>java Test42 24 88
The GCD of 24 and 88 is: 8
the LCM of them is:264 |
|