Java solves some of the large number problems in ACM

Source: Internet
Author: User
Tags integer division

The preferred scale for the results of arithmetic operations in large numbers

arithmetic preferred scale for results
Add Max (Addend.scale (), Augend.scale ())
Reducing Max (Minuend.scale (), Subtrahend.scale ())
By Multiplier.scale () + Multiplicand.scale ()
Except Dividend.scale ()-Divisor.scale ()
Problem D:integer Inquiry time limit:1 Sec Memory limit:128 MB
submit:59 solved:18
[Submit] [Status] [Web Board] Description

One of the first users of BIT ' s new supercomputer was Chip Diller. He extended his exploration of powers's 3 to go from 0 to 333 and he explored taking various sums of those numbers.

' This supercomputer is great, ' remarked Chip. "I only wish Timothy were" here for see these results. " (Chip moved to a new apartment, once one became available on the third floor of the Lemon Sky Apartments on third Street.)

Input

The input would consist of the lines of text, each of which contains a single verylonginteger. Each verylonginteger'll be is fewer characters in length, and would only contain digits (no verylonginteger would be n egative).

The final input line would contain a single zero in a line by itself.

Output

Your program should output the sum of the verylongintegers given in the input.

Sample Input
1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678900
Sample Output
370370367037037036703703703670
HINT

The large number is added, and the input 0 o'clock output is encountered until EOF (end of file) is read

Code:

1 ImportJava.math.BigInteger;2 ImportJava.util.Scanner;3  4  Public classmain{5      Public Static voidMain (string[] args) {6Scanner s=NewScanner (system.in);7BigInteger a,b=NewBigInteger ("0");8          while(S.hasnext ()) {9A=S.nextbiginteger (); Receive inputTen             if(! (a.tostring () = = ("0")) //Determine whether equal to 0 Oneb=B.add (a);  A             if(a.tostring () = = ("0")){ - System.out.println (b); -b=NewBigInteger ("0"); the             } -         }    -     } -}

Problem e:product time limit:1 Sec Memory limit:128 MB
submit:38 solved:25
[Submit] [Status] [Web Board] Description

The problem is to multiply, integers X, Y. (0<=x,y<10250)

Input

The input would consist of a set of pairs of lines. Each line in pair contains one multiplyer.

Output

For each input pair of lines the output line should consist one integer the product.

Sample Input
12122222222222222222222222222
Sample Output
144444444444444444444444444
HINT

Multiply two large numbers until EOF (end of file) is read

Code:

1 ImportJava.math.BigInteger;2 ImportJava.util.Scanner;3 4  Public classMain {5      Public Static voidMain (string[] args) {6Scanner s=NewScanner (system.in);7          while(S.hasnext ()) {8BigInteger a=S.nextbiginteger ();9BigInteger b=S.nextbiginteger ();TenBigInteger c=a.multiply (b); One System.out.println (c); A         } -     } -}

Problem f:exponentiation time limit:1 Sec Memory limit:128 MB
Submit:9 Solved:6
[Submit] [Status] [Web Board] Description

Problems involving the computation of exact values of very large magnitude and precision are common. For example, the computation of the national debt are a taxing experience for many computer systems.

This problem requires so you write a program to compute the exact value of Rn where R is a real number (0.0 < R < 99.999) and n is a integer such that $ < n \le 25$.

Input

The input would consist of a set of pairs of values for R and N. The R value would occupy columns 1 through 6, and the N value would be in columns 8 and 9.

Output

The output would consist of one line for each line of input giving the exact value of Rn. Leading zeros and insignificant t Railing zeros should is suppressed in the output.

Sample Input
95.123 120.4321 205.1234 156.7592  998.999 101.0100 12
Sample Output
548815620517731830194541.899025343415715973535967221869852721.00000005148554641076956121994511276767154838481760200726351 20383542976301346240143992025569.92857370126648804114665499331870370751166629547672049395302429448126.7641210216181644302 0690903717327667290429072743629540498.1075960194566517745610440100011.126825030131969720661201
HINT

n Power of large number A, until EOF (end of file) is read, ignoring 0 after decimal

Code:

1 ImportJava.math.BigDecimal;2 ImportJava.util.Scanner;3 4  Public classproblem_f_exponentiation {5 6      Public Static voidMain (string[] args) {7Scanner s=NewScanner (system.in);8 BigDecimal A;9         intb;Ten String C; One          while(S.hasnext ()) { AA=s.nextbigdecimal (); -b=s.nextint (); -A=A.pow (b). Striptrailingzeros (); Striptrailingzeros 0 Removal of the resulting fractional portion of the tail theC=a.toplainstring (); Toplainstring The resulting large number of results is not shown in scientific notation and converted to a string -             if(C.charat (0) = = ' 0 ')//Use Charat () to determine whether the first character is 0 -C=c.substring (1); - System.out.println (c); +         } -     } +}

Problem G:if We were a child againtime limit:1 Sec Memory limit:128 MB
Submit:7 Solved:6
[Submit] [Status] [Web Board] Description

The problem

The first project for the poor student is to make a calculator that can just perform the basic arithmetic operations.


But like many and university students he doesn ' t like to do any project by himself. He just wants to collect programs from here and there. As you is a friend of him, he asks you to write the program. But, you is also intelligent enough to tackle this kind of people. You agreed to write only the (integer) Division and mod (% in C + +) operations for him.

Input

Input is a sequence of lines. Each line would contain an input number. One or more spaces. A sign (division or MoD). Again spaces. and another input number. Both the input numbers is non-negative integer. The first one may be arbitrarily long. The second number n would be is in the range (0 < n < 231).

Output

A line for each input, each containing an integer. See the sample input and output. Output should not contain any extra space.

Sample Input
110/10099% 102147483647/21474836472147483646 2147483647
Sample Output
1912147483646
HINT

Large number for remainder and integer division operation

Code:

1 ImportJava.math.BigInteger;2 ImportJava.util.Scanner;3  Public classMain {4      Public Static voidMain (string[] args) {5Scanner s=NewScanner (system.in);6BigInteger a,b,t=NewBigInteger ("1");7 String C;8          while(S.hasnext ()) {9A=S.nextbiginteger ();TenC=S.next (); Oneb=S.nextbiginteger (); A             if(C.equals ("%")) t=A.mod (b);  -             if(C.equals ("/")) t=A.divide (b); - System.out.println (t); the         } -     } -}

Java solves some of the large number problems in ACM

Related Article

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.