头文件:
import java.util.*;
import java.math.*;
Ⅰ基本函数:
1.valueOf(parament); 将参数转换为制定的类型比如 int a=3;
BigInteger b=BigInteger.valueOf(a);
则b=3;
String s=”12345”;
BigInteger c=BigInteger.valueOf(s);
则c=12345;
2.add(); 大整数相加
BigInteger a=new BigInteger(“23”);
BigInteger b=new BigInteger(“34”);
a. add(b);
3.subtract(); 相减
4.multiply(); 相乘
5.divide(); 相除取整
6.remainder(); 取余
7.pow(); a.pow(b)=a^b
8.gcd(); 最大公约数
9.abs(); 绝对值
10.negate(); 取反数
11.mod(); a.mod(b)=a%b=a.remainder(b);
12.max(); min();
13.punlic int comareTo();
14.boolean equals(); 是否相等
15.BigInteger构造函数:
在Java中有两个类BigInteger和BigDecimal分别表示不可变的任意精度的整数和不可变的有符号的任意精度的十进制数(浮点数)。主要用于高精度计算中。这两个类使得java中的大数,高精度运算变得很简单,至于两个类的对象能表示最大范围不清楚,理论上能够表示无线大的数,只要计算机内存足够大。
这两个类都在java.math.*包中,因此每次必须在开头处引用该包。
Ⅱ.基本常量:
A=BigInteger.ONE 1
B=BigInteger.TEN 10
C=BigInteger.ZERO 0
Ⅲ.基本操作
1. 读入:
用Scanner类定义对象进行控制台读入,Scanner类在java.util.*包中
Scanner cin=new Scanner(System.in);// 读入
while(cin.hasNext()){ // 等同于!=EOF
int n;
BigInteger m;
n = cin.nextInt(); // 读入一个int;
m = cin.BigInteger();// 读入一个BigInteger;
System.out.print(m.toString());
}
Ⅳ.运用
HDU 1002
a+b大数版
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
void solve () {
BigInteger a, b, c;
Scanner cin = new Scanner(System.in);
int t = cin.nextInt ();
for (int i = 1; i <= t; i++) {
System.out.println ("Case " + i + ":");
a = cin.nextBigInteger ();
b = cin.nextBigInteger ();
System.out.println (a + " + " + b + " = " + a.add (b));
if (i != t) System.out.println ();
}
}
public static void main (String[] args) {
Main work = new Main();
work.solve ();
}
}
HDU 1042
阶乘大数版
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
int maxn = 10005;
void solve () {
Scanner cin = new Scanner(System.in);
int n;
while (cin.hasNext()) {
n = cin.nextInt ();
BigInteger ans = BigInteger.valueOf (1);
for (int i = 2; i <= n; i++) {
ans = ans.multiply (BigInteger.valueOf (i));
}
System.out.println (ans);
}
}
public static void main (String[] args) {
Main work = new Main();
work.solve ();
}
}
斐波那契数列大数版
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
void solve () {
Scanner cin = new Scanner(System.in);
BigInteger f1, f2, f3, f4, ans;
while (cin.hasNext ()) {
int n = cin.nextInt ();
f1 = BigInteger.valueOf (1);
f2 = f3 = f4 = ans = f1;
if (n <= 4) {
System.out.println ("1");
continue;
}
for (int j = 5; j <= n; j++) {
ans = f1.add (f2.add (f3.add (f4)));
f1 = f2;
f2 = f3;
f3 = f4;
f4 = ans;
}
System.out.println (ans);
}
}
public static void main (String[] args) {
Main work = new Main();
work.solve ();
}
}
f(n)=f(n−1)+f(n−2)+f(n−4)f(n)=f(n−1)+f(n−2)+f(n−4)
import java.math.*;
import java.util.*;
public class Main {
void solve () {
Scanner cin = new Scanner(System.in);
BigInteger[] ans = new BigInteger[1001];
ans[1] = BigInteger.valueOf (1);
ans[2] = BigInteger.valueOf (2);
ans[3] = BigInteger.valueOf (4);
ans[4] = BigInteger.valueOf (7);
for (int i = 5; i <= 1000; i++) {
ans[i] = ans[i-1].add (ans[i-2].add (ans[i-4]));
}
while (cin.hasNext ()) {
int n = cin.nextInt ();
System.out.println (ans[n]);
}
}
public static void main (String[] args) {
Main work = new Main();
work.solve ();
}
}
HDU 1715
还是斐波那契数列
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
void solve () {
Scanner cin = new Scanner(System.in);
int t = cin.nextInt ();
BigInteger f1, f2, f3;
for (int i = 0; i < t; i++) {
int n = cin.nextInt ();
f1 = BigInteger.valueOf (1);
f2 = BigInteger.valueOf (1);
f3 = BigInteger.valueOf (0);
if (n == 1 || n == 2) {
System.out.println ("1");
continue;
}
for (int j = 3; j <= n; j++) {
f3 = f1.add (f2);
f1 = f2;
f2 = f3;
}
System.out.println (f3);
}
}
public static void main (String[] args) {
Main work = new Main();
work.solve ();
}
}
高精度小数,要去掉末尾的后导0.
import java.math.*;
import java.util.*;
public class Main {
void solve () {
//BigInteger a, b, c;
Scanner cin = new Scanner(System.in);
BigDecimal a = BigDecimal.valueOf (0);
BigDecimal b = BigDecimal.valueOf (0);
while (cin.hasNext ()) {
a = cin.nextBigDecimal ();
b = cin.nextBigDecimal ();
System.out.println (a.add (b).stripTrailingZeros().toPlainString());
}
}
public static void main (String[] args) {
Main work = new Main();
work.solve ();
}
}
f(n)=f(n−1)+f(n−2)f(n)=f(n−1)+f(n−2)
import java.math.*;
import java.util.*;
public class Main {
void solve () {
BigInteger a, b, c;
Scanner cin = new Scanner(System.in);
int t = cin.nextInt ();
String s;
for (int l = 1; l <= t; l++) {
s = cin.next ();
int n = s.length ();
if (n == 1 || n == 2) {
System.out.println (n);
continue;
}
BigInteger f1 = BigInteger.valueOf (1);
BigInteger f2 = BigInteger.valueOf (2);
BigInteger f3 = BigInteger.valueOf (0);
for (int i = 3; i <= n; i++) {
f3 = f1.add (f2);
f1 = f2;
f2 = f3;
}
System.out.println (f3);
}
}
public static void main (String[] args) {
Main work = new Main();
work.solve ();
}
}