Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to subtract large integers without using the BigInteger that comes with the system

2025-04-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to achieve large integer subtraction without using the BigInteger that comes with the system". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let the editor take you to learn how to subtract large integers without using the BigInteger that comes with the system.

The first topic: string segmentation, do not use the system's own segmentation function.

Public class Myspilt {public static void main (String [] args) {String source = "abcdabeefabfgeeabcfffdsfsa"; / / abcdabeefabfgeeabcfffdsfsa String target = "ab"; myspilt (source, target);} public static void myspilt (String source, String target) {char [] sourceCharArray = source.toCharArray (); char [] targetCharArray = target.toCharArray (); StringBuffer stringBuffer = new StringBuffer (); List result = new ArrayList () For (int I = 0; I

< source.length(); i++) { int j = 0; for (; j < target.length() && i + j < source.length(); j++) { if (sourceCharArray[i + j] != targetCharArray[j]) { break; } } if (j == targetCharArray.length) { if (stringBuffer.length() >

0) {result.add (stringBuffer.toString ()); stringBuffer.setLength (0);} I = I + j-1;} else {stringBuffer.append (sourceCharley [I]) }} if (stringBuffer.length () > 0) {result.add (stringBuffer.toString ());} System.out.println ("end");}}

The second question: large integer subtraction, do not use the system's own BigInteger to calculate.

Class App1 {/ / calculate a-b public static void main (String [] args) {String a = "454656875457421545474578457"; String b = "12454578787845474545454545784578474545457845784745938271458457214"; mySubtract (a, b); String A1 = "1000000000000000000"; String b1 = "999999999"; mySubtract (A1, b1);} public static void mySubtract (String a, String b) {BigInteger A1 = new BigInteger (a) BigInteger b1 = new BigInteger (b); System.out.println (a1.subtract (b1). ToString ()); boolean altb = false; if (a.length ())

< b.length() || (a.length() == b.length() && a.compareTo(b) < 0)) { altb = true; String tmp = a; a = b; b = tmp; } boolean borrow = false; StringBuilder sb = new StringBuilder(a.length()); for (int i = a.length() - 1, j = b.length() - 1; i >

= 0; iMurray, Jmuri -) {char char1 = a.charAt (I); char char2 = '0mm; if (j > = 0) {char2 = b.charAt (j);} if (borrow) {char1-- } if (char1 < char2) {borrow = true; sb.append (char1 + 10-char2);} else {borrow = false; sb.append (char1-char2);}} String result = sb.reverse () .toString () Result = result.replaceAll ("^ [0] +", ""); if (result.length () = = 0) {result = "0";} if (altb) {result = "-" + result;} System.out.println (result) }} at this point, I believe you have a deeper understanding of "how to achieve large integer subtraction without using the BigInteger that comes with the system". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report