public static int Gcd(int a, int b) {
int re = 0;
long st = System.currentTimeMillis();
while (b > 0) {
re = a % b;
a = b;
b = re;
}
long end = System.currentTimeMillis();
System.out.println("totalTime"+(end-st));
return a;
}
//自己写的算法求最大公约数
public static int max(int a, int b) {
long st = System.currentTimeMillis();
for (int i = Math.min(a, b); i >= 1; i--) {
if (a % i == 0 && b % i == 0) {
long end = System.currentTimeMillis();
System.out.println("totalTime"+(end-st));
return i;
}
}
return 0;
int re = 0;
long st = System.currentTimeMillis();
while (b > 0) {
re = a % b;
a = b;
b = re;
}
long end = System.currentTimeMillis();
System.out.println("totalTime"+(end-st));
return a;
}
//自己写的算法求最大公约数
public static int max(int a, int b) {
long st = System.currentTimeMillis();
for (int i = Math.min(a, b); i >= 1; i--) {
if (a % i == 0 && b % i == 0) {
long end = System.currentTimeMillis();
System.out.println("totalTime"+(end-st));
return i;
}
}
return 0;
}
当a,b相对较大的时候,两种算法计算的时间相差很大