JAVA big value problem summary, JAVA numerical problem summary

Source: Internet
Author: User

JAVA big value problem summary, JAVA numerical problem summary

Big value problems:



If the precision of the basic integers (such as int and long) and floating-point numbers is not satisfactory, you can use java. the math package contains two classes: BigInteger (integer of any precision) and BigDecimal (floating point of any precision ).

These two classes can realize the numerical value of a person's length at a time.

BigInteger a = BigInteger. valueOf (100); // converts 100 to an integer of the large number type.
You can use the valueOf () method to convert a common value to a large number.

Then, how can we use a large number to calculate the value?

The addition method provided in JAVA is:

BigInteger sum = a.add(b);//c = a + b;
BigInteger ji = a. multiply (B. add (BigInteger. valueOf (2); // ji = a * (B + 2)


Note:

Unlike C ++, java only provides string connections, and the + operator is overloaded. java does not provide the operator overload function, there is no chance for java programmers to write Operator Overloading in their own classes.


Calculate the number of combinations:

c = c.multiply(BigInteger.valueOf(n-i+1).divide(BigInteger.valueOf(i)));//(n-i+1)/i
/** Description: There are n balls, and m balls can be captured in one hand. To learn how many capturing methods are available, run the "n" command with 0 input. The idea is to apply java. the BigInteger class in the math package uses. multiply (B) generates a * B product */import java. math. bigInteger; import java. util. role; public class Main {public static void main (String [] args) {role in = new role (System. in); int n = 1, m; // evaluate the combination of C (m, n) while (0! = N) {n = in. nextInt (); m = in. nextInt (); BigInteger c = BigInteger. valueOf (1); for (int I = 1; I <= m; I ++) {c = c. multiply (BigInteger. valueOf (n-I + 1 )). divide (BigInteger. valueOf (I);} System. out. println (c );}}}


BigInteger:

// BigInteger add (BigInteger it) // sum // BigInteger subtract (BigInteger it) // calculate the difference // BigInteger multiply (BigInteger it) // accumulate // BigInteger divide (BigInteger it) // evaluate the quotient // BigInteger mod (BigInteger it) // evaluate the modulo // int compareTo (BigInteger it) // it is used to determine the size of two large numbers. The large number is equal to the it of the other. The return value is 0, less than, and negative. The return value is greater than the return value of positive/static BigInteger. valueOf (long x) static BigInteger valueOf (long xx) // the return value is a big integer of xx.



BigDecimal:

BigDecimal add (BigDecimal it) // sum BigDecimal subtract (BigDecimal it) // evaluate BigDecimal multiply (BigDecimal it) // evaluate the product BigDecimal mod (BigDecimal it) // evaluate the modulo

Exceptions:

BigDecimal divide (BigDecimal it RoundingMode mode) 5.0 // The rounding method must be provided for calculation. For other Rounding Methods, see the API documentation static BigDecimal valueOf (long x) // The return value is equal to the large real number static BigDecimal valueOf (long x, int k) of x. // the return value is a large real number of x/(k power of 10 ).







JAVA big Value Problems

The question is, calculate 100! + 101! + ...... + 200!
The Code is as follows:
Import java. math .*;
Public class No0305
{
Public static void main (String args [])
{
BigInteger
First100 = new BigInteger ("100"), // initialize each big integer
Last201 = new bigint ("201 "),
One = new BigInteger ("1 "),
Power = new BigInteger ("1 "),
Result = new BigInteger ("0 ");
For (BigInteger I = one; I. compareTo (last201) <0; I = I. add (one) // cyclically accumulate from 1 to 201
{
Power = power. multiply (I); // calculate I!
If (I. compareTo (first100)> = 0) // from 100! Start to add
Result = result. add (power );
}
System. out. println (result. toString ());
}
}

Explanation of the large value of java program

Import java. math .*;
Import java. util .*;
These two lines are the existing classes in the call API documentation. In fact, the full names can be written as follows:
Import java. math. BigInteger;
Import java. util. collections;

Because the BigInteger and delimiter methods you call are called from the above math and util packages, you must specify them; otherwise, an error occurs.

Running in = new Processing (System. in );
This is a statement you want to enter.
The API documentation describes the following:
The separator mode is used to separate the input into tags. By default, the separator mode matches the blank. Then, you can use different next methods to convert the resulting tag to different types of values.

For example, the following code enables a user to read a number from System. in:

Pipeline SC = new pipeline (System. in );
Int I = SC. nextInt ();
Let's look at another example. The following Code enables the long type to be allocated through the items in the myNumbers file:

Using SC = new users (new File ("myNumbers "));
While (SC. hasNextLong ()){
Long aLong = SC. nextLong ();
}

For
BigInteger lotteryOdds = BigInteger. valueOf (1 );
The valueOf () method in the API documentation. The method is as follows:

ValueOf
Public static BigInteger valueOf (long val) returns the BigInteger whose value is equal to the value of the specified long. The "static factory method" provided takes precedence over the (long) constructor because the former allows reuse of frequently used BigInteger.

Parameters:
Val-the value of BigInteger to be returned.
Return Value:
BigInteger with the specified value.
-----------------------------------------

The above is the key point of this program. In fact, there is no knowledge point, that is, querying java API documentation.
You should be a beginner, because you don't feel like using documents. Then you can search for API documents on the Internet, including Chinese and English. After downloading the documents, you will learn how to use them, about 40 MB.
API documentation is a required document for learning java and future java development. For example, in your program above, I didn't know the BigInteger class and valueOf () method at first, but you will use it after learning about the document. When you program, you need a mathematical method.
Search for the java. math package and view each class so that you can know which method to use.
Remember: no one can remember all the methods in the API documentation, and it makes no sense to remember them. API documentation is equivalent to a dictionary.

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.