日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

sqrt开平方算法解析

發布時間:2024/4/15 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 sqrt开平方算法解析 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

昨天筆試遇到一題,要求用java實現sqrt,當時就想,哪里管過怎么實現的,都是直接拿來用的。所以晚上就查了一些資料,將實現過程整理如下:

圖示:

?

算法思路說明,下面的碎片被開方數”,補丁平方根”是為了方便稱呼自取的名稱

  • 1.將被開方數n從右向左兩位一劃分,例如將10517049劃分為10 51 70 49(可能是因為n的平方根肯定是n位數的一半吧,沒找到解釋的相關資料);
  • 2.獲取碎片被開方數”fragmentSqrt。從左向右每次取劃分好的兩位數,fragmentSqrt在第一次取值時就是n的最高一組兩位數(這里是10),此后就是第4步計算的余數remainder和取到的兩位數拼接(例如1 51,27 70,194 49);
  • 3.推測fragmentSqrt的“補丁平方根”patchRoot的個位bit_patchRoot

????????? 推測公式為(high_patchRoot×2×10+bit_patchRootbit_patchRoot,使其盡可能小于等于fragmentSqrt,循環bit_patchRoot從9~1即可。high_patchRoot為上一輪的補丁平方根”patchRoot

????????? 推測公式的解釋:以1156為例,易觀察到它的平方根是兩位,十位是3,設個位是a,則(3×10+a)2=1156,即302+2×3×10a+a2=1156,即3×2×10a+a2=256,3×2×10a+a2變形為(3×2×10+a)a,即為推測公式。故2為(x+y)2=x2+2xy+y2中的2,×10表示是十位數。

  • 4.計算余數remainder;

?????????? remainder=fragmentSqrt-(high_patchRoot×2×10+bit_patchRoot)×bit_patchRoot

  • 5.更正“補丁平方根patchRoot;

?????????? patchRoot=high_patchRoot×10+bit_patchRoot

  • 6.返回第二步

①151為“碎片被開方數”fragmentSqrt

②2為bit_patchRoot

③3為high_patchRoot,即上一輪的patchRoot

④27為余數remainder

⑤32位patchRoot

①~⑤是第二輪結果

?代碼

?

package kafka.productor;import java.math.BigInteger; import java.util.Scanner;public class MySqrt {static final BigInteger NUM20 = BigInteger.valueOf(20);// 將后面使用的參數定義為final常量public static void main(String[] args) {MySqrt mySqrt = new MySqrt();Scanner input = new Scanner(System.in);System.out.println(mySqrt.sqrt(input.next()));}public String sqrt(String n){String patchRoot="0"; // 平方根初始化為字符串0String remainder=""; //余數初始化為""if(n.length()%2 != 0) n = "0"+n; //如果n是奇數為,防止substring越界for(int i=0; i<n.length()/2; i++){//兩兩分組String fragmentSqrt = remainder+n.substring(2*i,2*i+2); //第2步String high_patchRoot = patchRoot;String bit = getBit(new BigInteger(high_patchRoot),new BigInteger(fragmentSqrt)); //第3步remainder = getRemainder(new BigInteger(fragmentSqrt),new BigInteger(high_patchRoot),new BigInteger(bit)); //第4步patchRoot = high_patchRoot+bit; //第5步}return patchRoot.substring(1); // 去掉結果之前的0}private String getRemainder(BigInteger fragmentSqrt, BigInteger high_patchRoot, BigInteger bit_patchRoot) {return fragmentSqrt.subtract(high_patchRoot.multiply(NUM20).add(bit_patchRoot).multiply(bit_patchRoot)).toString();}private String getBit(BigInteger high_patchRoot,BigInteger fragmentSqrt) {int i;for(i=9; i>0; i--){BigInteger bi = BigInteger.valueOf(i);if (fragmentSqrt.compareTo(high_patchRoot.multiply(NUM20).add(bi).multiply(bi))>=0) break;}return String.valueOf(i);} }

?參考

https://blog.csdn.net/Super2333/article/details/79476149

https://baike.baidu.com/item/%E5%BC%80%E5%B9%B3%E6%96%B9%E8%BF%90%E7%AE%97/1165387?fr=aladdin

為了得到而努力

2019-03-07

轉載請注明來處。

轉載于:https://www.cnblogs.com/malw/p/10486659.html

總結

以上是生活随笔為你收集整理的sqrt开平方算法解析的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。