TA的每日心情 | 开心 2021-3-12 23:18 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
- C语言趣味程序百例精解之JAVA实现(42)最大公约数&最小公倍数
- [img]http://img.javaxxz.com/2014/11/18/000610531.jpg[/img]
- 程序:
- 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
- ",a,b,n);
- System.out.printf("the LCM of them is:%d
- ",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
复制代码
源码下载:http://file.javaxxz.com/2014/11/18/000610671.zip |
|