# java写大数总结

## 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 ();
}
}

## 阶乘大数版

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++) {
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++) {
}
while (cin.hasNext ()) {
int n = cin.nextInt ();
System.out.println (ans[n]);
}
}
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);
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++) {
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 ();
}
}
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++) {
f1 = f2;
f2 = f3;
}
System.out.println (f3);
}
}
public static void main (String[] args) {
Main work = new Main();
work.solve ();
}
}

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120