import java.util.*;
public class Test40014 {
public static void main(String []args){
Scanner in =new Scanner(System.in);
int gcd, lcm, m, n,r;
int repeat, ri;
repeat=in.nextInt();
for(ri = 1; ri <= repeat; ri++){
m=in.nextInt();
n=in.nextInt();
if(m <= 0 || n <= 0)
System.out.println("m <= 0 or n <= 0");
else{
/*---------*/
int yu,tmp;
int ji = m*n;
if(m
tmp=m;
m=n;
n=tmp;
}
while((yu=m%n)!=0)
{
m=n;
n = yu;
}
gcd=n;
lcm=ji/n;
System.out.println("the least common multiple:"+lcm+", the greatest common divisor:"+gcd);
}
}
}
}
你好楼主!
希望以下代码对你有帮助:
import java.util.*;
public class Test40014 {
public static void main(String []args){
Scanner in =new Scanner(System.in);
int gcd, lcm, m, n,r;
int repeat, ri;
repeat=in.nextInt();
for(ri = 1; ri <= repeat; ri++){
m=in.nextInt();
n=in.nextInt();
if(m <= 0 || n <= 0)
System.out.println("m <= 0 or n <= 0");
else{
gcd = cd(m,n) ;
lcm =( m*n/gcd );//最小公倍数等于2数之积除以最大公约数
System.out.print("the least common multiple:"+lcm) ;
System.out.println(" ,the greatest common divisor:"+gcd);
}
}
}
public static int cd(int m,int n)
{ //用迭代思想求最大公约数
if(m%n==0)
return n ;
else
return cd(n,m%n) ;
}
}