首页 > 其他 > 详细

蓝桥杯 矩阵翻硬币

时间:2017-04-02 19:30:40      阅读:249      评论:0      收藏:0      [点我收藏+]

思路:

参考了http://blog.csdn.net/snailset/article/details/26752435,思路很清晰。

用java实现了高效的牛顿迭代法。

我tm都要爱上java了。

实现:

 1 import java.math.*;
 2 import java.util.*;
 3 
 4 public class Main {
 5 
 6     public static BigInteger sqrt(BigInteger x){
 7         if (x .equals(BigInteger.ZERO) || x.equals(BigInteger.ONE)) {
 8             return x;
 9         }
10         BigInteger two = BigInteger.valueOf(2L);
11         BigInteger y;
12         for (y = x.divide(two);
13              y.compareTo(x.divide(y)) > 0;
14              y = ((x.divide(y)).add(y)).divide(two));
15         return y;
16     }
17 
18     /**
19      * @param args
20      */
21     public static void main(String[] args) {
22         // TODO Auto-generated method stub
23         BigInteger n;
24         Scanner s = new Scanner(System.in);
25         BigInteger a = s.nextBigInteger();
26         BigInteger b = s.nextBigInteger();
27         BigInteger ia = sqrt(a);
28         BigInteger ib = sqrt(b);
29         System.out.println(ia.multiply(ib));
30     }
31 }

 

蓝桥杯 矩阵翻硬币

原文:http://www.cnblogs.com/wangyiming/p/6659525.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!