大数A+B
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int T;
T = in.nextInt();
for(int cas = 1;cas <= T;cas++ )
{
System.out.println("Case "+cas+":");
BigInteger a = in.nextBigInteger();
BigInteger b = in.nextBigInteger();
BigInteger c = a.add(b);
System.out.println(a+" + "+b+" = "+c);
if(cas!=T)System.out.println("");
}
}
}
- HDU-1042(https://cn.vjudge.net/problem/HDU-1042)
求N!
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int T;
while(in.hasNext())
{
int n = in.nextInt();
BigInteger ans = new BigInteger("1");
for(int i=1;i<=n;i++)
{
BigInteger a=BigInteger.valueOf(i);
ans = ans.multiply(a);
}
System.out.println(ans);
}
}
}
- HDU - 1133
公式:(m+n)!*(m-n+1)/(m+1)
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int cas = 0;
while(in.hasNext())
{
cas++;
int m = in.nextInt();
int n = in.nextInt();
if(n==0&&m==0)
{
break;
}
if(m<n)
{
System.out.println("Test #"+cas+":");
System.out.println("0");
continue;
}
BigInteger ans = new BigInteger("1");
for(int i=1;i<=n+m;i++)
{
BigInteger a=BigInteger.valueOf(i);
ans = ans.multiply(a);
}
BigInteger a=BigInteger.valueOf(m-n+1);
ans = ans.multiply(a);
a=BigInteger.valueOf(m+1);
ans = ans.divide(a);
System.out.println("Test #"+cas+":");
System.out.println(ans);
}
}
}
- HDU - 1250
公式:F(1) = 1, F(2) = 1, F(3) = 1,F(4) = 1, F(n>4) = F(n - 1) + F(n-2) + F(n-3) + F(n-4)
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
BigInteger f[] = new BigInteger[10005];
while(in.hasNext())
{
int n = in.nextInt();
f[1] = BigInteger.valueOf(1);
f[2] = BigInteger.valueOf(1);
f[3] = BigInteger.valueOf(1);
f[4] = BigInteger.valueOf(1);
for(int i=5;i<=n;i++)
{
f[i]=f[i-1].add(f[i-2]).add(f[i-3]).add(f[i-4]);
}
System.out.println(f[n]);
}
}
}
HDU - 1297
f[i] = f[i - 1].add(f[i - 2]).add(f[i - 4]);
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
BigInteger f[] = new BigInteger[10005];
//f[n] = f[n - 1] + f[n - 2] + f[n - 4]
f[0] = BigInteger.valueOf(1);
f[1] = BigInteger.valueOf(1);
f[2] = BigInteger.valueOf(2);
f[3] = BigInteger.valueOf(4);
for(int i=4;i<=1000;i++)
{
f[i] = f[i - 1].add(f[i - 2]).add(f[i - 4]);
}
while(in.hasNext())
{
int n = in.nextInt();
System.out.println(f[n]);
}
}
}
Fibonacci数列
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
BigInteger f[] = new BigInteger[10005];
//f[n] = f[n - 1] + f[n - 2] + f[n - 4]
f[0] = BigInteger.valueOf(0);
f[1] = BigInteger.valueOf(1);
for(int i=2;i<=1000;i++)
{
f[i] = f[i - 1].add(f[i - 2]);
}
int T;
T = in.nextInt();
for(int cas=1;cas<=T;cas++)
{
int n = in.nextInt();
System.out.println(f[n]);
}
}
}
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while(in.hasNext())
{
BigDecimal a = in.nextBigDecimal();
BigDecimal b = in.nextBigDecimal();
BigDecimal c = a.add(b);
c = c.stripTrailingZeros();
String s=c.toPlainString();
System.out.println(s);
}
}
}
/*
一个是 BigDecimal.stripTrailingZeros()
,作用是将BigDecimal转化为最简形式(去掉末尾多余的0或小数点)
还有 BigDecimal.toPlainString() ,
作用是将BigDecimal转为字符串,因为小数位数过多的话会被输出成科学计数法表示,
不希望那样的话就要用到这个方法;
*/
f[i] = f[i-1] + f[i-2]
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
BigInteger f[] = new BigInteger[1000];
f[1] = BigInteger.valueOf(1);
f[2] = BigInteger.valueOf(2);
for(int i=3;i<=200;i++)
{
f[i] = f[i-1].add(f[i-2]);
}
int T;
T = in.nextInt();
for(int i=1;i<=T;i++)
{
String s = in.next();
System.out.println(f[s.length()]);
}
}
}
/*
一个是 BigDecimal.stripTrailingZeros()
,作用是将BigDecimal转化为最简形式(去掉末尾多余的0或小数点)
还有 BigDecimal.toPlainString() ,
作用是将BigDecimal转为字符串,因为小数位数过多的话会被输出成科学计数法表示,
不希望那样的话就要用到这个方法;
*/