算法
来源:互联网 发布:ubuntu 设置密码 编辑:程序博客网 时间:2024/06/13 23:45
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相对较大的时候,两种算法计算的时间相差很大
阅读全文
0 0
- 算法
- 算法
- 算法
- 算法
- 算法
- 算法
- 算法
- 算法
- 算法
- 算法
- 算法
- 算法
- 算法
- 算法
- 算法
- 算法
- 算法
- 算法
- 嵌入式视频方案学习第十一篇——视频编码模块VENC
- JVM对象的创建与内存管理
- springboot集成hibernate-jpa方式
- protothread_简介
- 开发中泛型使用注意事项
- 算法
- 使用@RequestMapping 定义处理器函数-支持的形参类型
- MySQL行级锁、表级锁、页级锁
- Frogs (hdu5514)——2015ACM/ICPC亚洲区沈阳站(容斥定理)
- PINDEL检测结构变异
- 主题三 编译过程介绍---- 16.宏定义与使用分析
- 【已解决】linux redhat 6 如何打开防火墙中的某个端口?例如:5900端口
- Java静态代码块和构造块(面试时候能用到,开发一般不会用到)
- android binder机制,注册系统服务---结果返回