package trace01;
import java.util.Scanner;
public class gsaDemo {
public static void main(String[] args){
Scanner scanner=new Scanner(System.in);
System.out.println("请输入求最大公约数的两个数:");
System.out.println("请输入第一个数 m:");
int m;
m=scanner.nextInt();
System.out.println("请输入第二个数 n:");
int n;
n=scanner.nextInt();
long begintime=System.currentTimeMillis();
int gcds=gcds(m,n);
System.out.println("最大公约数为:"+gcds);
long endtime=System.currentTimeMillis();
System.out.println("递归方法的调用时间为:"+(endtime-begintime));
long begintime2=System.currentTimeMillis();
int gcdx=gcdx(m,n);
System.out.println("最大公约数为:"+gcdx);
long endtime2=System.currentTimeMillis();
System.out.println("循环方法的调用时间为:"+(endtime2-begintime2));
}
public static int gcds(int m,int n)
{
if(n==0)
return m;
else
return gcds(n,m%n);
}
public static int gcdx(int m,int n)
{
int a;
while(n!=0)
{
a=m%n;
m=n;
n=a;
}
return m;
}
import java.util.Scanner;
public class gsaDemo {
public static void main(String[] args){
Scanner scanner=new Scanner(System.in);
System.out.println("请输入求最大公约数的两个数:");
System.out.println("请输入第一个数 m:");
int m;
m=scanner.nextInt();
System.out.println("请输入第二个数 n:");
int n;
n=scanner.nextInt();
long begintime=System.currentTimeMillis();
int gcds=gcds(m,n);
System.out.println("最大公约数为:"+gcds);
long endtime=System.currentTimeMillis();
System.out.println("递归方法的调用时间为:"+(endtime-begintime));
long begintime2=System.currentTimeMillis();
int gcdx=gcdx(m,n);
System.out.println("最大公约数为:"+gcdx);
long endtime2=System.currentTimeMillis();
System.out.println("循环方法的调用时间为:"+(endtime2-begintime2));
}
public static int gcds(int m,int n)
{
if(n==0)
return m;
else
return gcds(n,m%n);
}
public static int gcdx(int m,int n)
{
int a;
while(n!=0)
{
a=m%n;
m=n;
n=a;
}
return m;
}
}