Multiply Java Edition by large number

Source: Internet
Author: User

When engaged in ACM encountered large number multiplication problem, on the Internet to find a bit, saw a C + + version of the

http://blog.csdn.net/jianzhibeihang/article/details/4948267

Made a version of Java

Here's the idea.

The number is received as a string, converted to a int[] integer array, and then num1[],num2[] multiplied, the result is saved to result[]

Other notes are noted in the notes

1  Packagecom.gxf.test;2 3 ImportJava.util.Scanner;4 5  Public classbigdatamultiply {6 7      Public Static voidMain (string[] args) {8         Final intN = 100;//multiply by 100-digit maximum9Scanner Scanner =NewScanner (system.in);Ten          One         intNum1[] = Getnum (scanner);//Save the first number 12345 A         intNum2[] = Getnum (scanner);//Save a second number 12345 -          -         intResult[] =New int[2 *N]; the         //The first number 12345 each bit multiplied by the second number 12345 each, the result is put into result[] -         //Note 5 times every digit is saved in result[0] [1] [2] [3] [4] -         //4 multiply each digit by the value in [1] [2] [3] [4] -          for(inti = 0; i < num1.length; i++){ +              for(intj = 0; J < Num2.length; J + +){ -Result[i + j] + = num1[i] *Num2[j]; +             } A}//The resulting results also require rounding and shift processing at         //rounding and Shift handling -          for(inti = 0; I < 2 * N-1; i++){ -Result[i + 1] + = Result[i]/10; -Result[i] = result[i]% 10; -         } -         //Find the result length in         intLength = 2 * N-1; -          while(Result[length] = = 0) tolength--; +         //output Note first output high-top bits are saved in the array -          for(inti = length; I >= 0; i--){ theSystem.out.print (Result[i] + ""); *         } $ scanner.close ();Panax Notoginseng     } -      Public Static int[] Getnum (Scanner Scanner) { the         Final intN = 100; +         intNum[] =New int[N]; A         Chararray_char[]; theString Str_num = ""; +         //Scanner Scanner = new Scanner (system.in);//for receiving input -          $Str_num =Scanner.next (); $Array_char = Str_num.tochararray ();//string 12345 has been converted into a character array -         //converts a character array to an int array -         intLength =array_char.length; the          for(inti = 0; i < array_char.length; i++){ -Num[length-i-1] = array_char[i]-' 0 ';//note here that the saved position is kept high in the array and the lows are saved in the array subscript.Wuyi             //because the back of the calculation is from the lower subscript the             //Num[i] = array_char[i] -         }         Wu         //scanner.close (); -         returnnum; About     } $}

PS: Why here result[] size is 2*n, assuming that two number is 3 digits, such as 565 * 898 can not be greater than 1000 * 1000, and 1000 * 1000 is exactly 2 * 3 = 6 bits

Multiply Java Edition by large number

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.