It is very troublesome to process large numbers in C or C ++, but there are two classes in Java: biginteger and bigdecimal, which respectively represent the big integer class and the big floating point class,
Objects of the two classes can represent the maximum range. Theoretically, they can represent a large number of wireless devices, as long as the computer memory is large enough. Both classes are in the Java. Math. * package, so the package must be referenced at the beginning each time.
I basic functions:
1. valueof (parament); convert the parameter to a specified type
For example, int A = 3;
Biginteger B = biginteger. valueof ();
Then B = 3;
String S = "12345 ";
Biginteger c = biginteger. valueof (s );
C = 12345;
2. Add (); add large Integers
Biginteger A = new biginteger ("23 ");
Biginteger B = new biginteger ("34 ");
A. Add (B );
3. Subtract (); Subtraction
4. Multiply (); multiply
5. Divide (); Division and rounding
6. remainder (); obtain the remainder
7. Pow (); A. Pow (B) = a ^ B
8. gcd (); maximum common approx.
9. Abs (); absolute value
10. Negate (); returns the inverse number.
11. Mod (); A. Mod (B) = A % B = A. remainder (B );
12. Max (); min ();
13. Public int comareto ();
14. boolean equals (); Equal
15. biginteger constructor:
The following two types are generally used:
Biginteger (string Val );
Converts a specified string to a decimal representation;
Biginteger (string Val, int Radix );
Converts the string representation of the biginteger of the specified base to biginteger.
Ii. Basic constants:
A = biginteger. One 1
B = biginteger. Ten 10
C = biginteger. Zero 0
III. Basic operations
1. Read:
Use the consumer class to define objects for console reading. The consumer class is in the Java. util. * package.
Cin = new partition (system. In); // read
While (CIN. hasnext () // equivalent! = EOF
{
Int N;
Biginteger m;
N = cin. nextint (); // read an int;
M = cin. nextbiginteger (); // read a biginteger;
System. Out. Print (M. tostring ());
}
Iv. Application
Four budgets:
Import java. util. collections;
Import java. Math .*;
Import java. Text .*;
Public class main
{
Public static void main (string ARGs [])
{
Cin = new partition (system. In );
Biginteger A, B;
Int C;
Char op;
String S;
While (CIN. hasnext ())
{
A = cin. nextbiginteger ();
S = cin. Next ();
OP = S. charat (0 );
If (OP = '+ ')
{
B = cin. nextbiginteger ();
System. Out. println (A. Add (B ));
}
Else if (OP = '-')
{
B = cin. nextbiginteger ();
System. Out. println (A. Subtract (B ));
}
Else if (OP = '*')
{
B = cin. nextbiginteger ();
System. Out. println (A. Multiply (B ));
}
Else
{
Bigdecimal A1, B1, EPS;
String S1, S2, temp;
S1 = A. tostring ();
A1 = new bigdecimal (S1 );
B = cin. nextbiginteger ();
S2 = B. tostring ();
B1 = new bigdecimal (S2 );
C = cin. nextint ();
EPS = a1.divide (B1, C, 4 );
System. Out. Print (A. Divide (B) + "" + A. Mod (B) + "");
If (C! = 0)
{
Temp = "0 .";
For (INT I = 0; I <C; I ++) temp + = "0 ";
Decimalformat GD = new decimalformat (temp );
System. Out. println (Gd. Format (EPS ));
}
Else system. Out. println (EPS );
}
}
}
}
Example: calculate the number of k elements in an nelement set: C (n, k) = n! /(K! * (N-k )!)
Import java. Math. biginteger;
Import java. util .*;
Import java. Io .*;
Public class factorialbigintegertest {
Public static void main (string [] ARGs) throws exception {
Cin = new partition (system. In );
Int T = cin. nextint ();
For (INT I = 0; I <t; I ++ ){
Int n = cin. nextint (), k = cin. nextint ();
Biginteger NF = new biginteger ("" + factorial (n ));
Biginteger kf = new biginteger ("" + factorial (k ));
Biginteger nk = new biginteger ("" + factorial (n-k ));
Biginteger n1 = new biginteger ("" + NF. Divide (kf ));
System. Out. println (n1.divide (NK ));
}
}
Private Static final biginteger factorial (int n ){
Biginteger = new biginteger ("" + n );
If (n = 0)
Return biginteger. One;
Else
Return factorial (n-1). Multiply (biginteger );
}
}
Enter 2000 50
Running result:
19961695734279236211032903597262781251004330223561192760088208846391036329722665786642280715824928160