您的位置:首页 > 编程语言 > Java开发

Java大数类

2016-05-29 19:44 761 查看

原文:http://blog.csdn.net/niushuai666/article/details/6972991

大数阶乘

题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=28

代码如下:

import java.io.*;
import java.math.BigInteger;
import java.util.*;

public class Main
{
public static void main(String args[])
{
Scanner cin = new Scanner(System.in);
int n = cin.nextInt();
BigInteger ans = BigInteger.ONE;
for(int i = 1; i <= n; ++i)
ans = ans.multiply(BigInteger.valueOf(i));
System.out.println(ans);
}
}


棋盘覆盖

题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=45

代码如下:

import java.math.BigInteger;
import java.util.*;
import java.io.*;

public class Main
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
int test = in.nextInt();
while(test-- > 0)
{
int n;
n = in.nextInt();
BigInteger a = new BigInteger("4");
for(int i = 1; i < n; ++i)
a = a.multiply(BigInteger.valueOf(4));
System.out.println(a.subtract(BigInteger.valueOf(1)).divide(BigInteger.valueOf(3)));
}
}
}


比较大小

题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=73

代码如下:

import java.io.*;
import java.math.BigInteger;
import java.util.*;

public class Main
{
public static void main(String args[])
{
Scanner cin = new Scanner(System.in);
while(cin.hasNext())
{
BigInteger a = cin.nextBigInteger();
BigInteger b = cin.nextBigInteger();
if(a.equals(BigInteger.ZERO) && b.equals(BigInteger.ZERO))
break;
int flag = a.compareTo(b);
if(flag == -1)
System.out.println("a<b");
else if(flag == 0)
System.out.println("a==b");
else
System.out.println("a>b");
}
}
}


大数加法

题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=103

代码如下:

import java.math.BigInteger;
import java.util.*;
import java.io.*;

public class Main
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
int n = in.nextInt();
for(int i = 1; i <= n; ++i)
{
BigInteger a = in.nextBigInteger();
BigInteger b = in.nextBigInteger();
BigInteger ans = a.add(b);
System.out.println("Case " + i + ":");
System.out.println(a + " + " + b + " = " +ans);
}
}
}


递推求值

题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=114

代码如下:

import java.io.*;
import java.math.BigInteger;
import java.util.*;

public class Main
{
public static void main(String args[])
{
Scanner cin = new Scanner(System.in);
BigInteger a[] = new BigInteger[100];
while(cin.hasNext())
{
for(int i = 0; i <= 2; ++i)
a[i] = cin.nextBigInteger();
for(int i = 3; i <= 99; ++i)
a[i] = a[i - 1].add(a[i - 2]).add(a[i - 3]);
System.out.println(a[99]);
}
}
}


高精度幂

题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=155

代码如下:

import java.io.*;
import java.math.BigDecimal;
import java.util.*;

public class Main
{
public static void main(String args[])
{
Scanner cin = new Scanner(System.in);
while(cin.hasNext())
{
BigDecimal ans = cin.nextBigDecimal();
int n = cin.nextInt();
String res = ans.pow(n).stripTrailingZeros().toPlainString(); //整数去掉小数点和后面的0
if(res.startsWith("0")) //去掉前导0
{
res = res.substring(1);
}
System.out.println(res);
}
}
}


Ps

刚开始学一点Java,方便处理大数

Scanner in = new Scanner(System.in); 从流读入

int n = in.nextInt(); 此处nextInt读入一个Int型,读入double要换为in.nextDouble

valueOf:赋初值

add:+ a.add(b);

subtract:-

multiply:*

divide:/

remainder:this % val

divideAndRemainder:a[0]=this / val; a[1]=this % val

pow:a.pow(b)=a^b

gcd,abs:公约数,绝对值

negate:取负数

signum:符号函数

mod:a.mod(b)=a%b;

shiftLeft:左移,this << n ,this*2^n;

shiftRight:右移,this >> n,this/2^n;

and:等同于c++的&&,且;

or:||,或;

xor:异或,BigInteger xor(BigInteger val),this^val

not:!,非;

bitLength:返回该数的最小二进制补码表示的位的个数,即 * 不包括 * 符号位 (ceil(log2(this <0 ? -this : this + 1)))。对正数来说,这等价于普通二进制表示的位的个数。

bitCount:返回该数的二进制补码表示中不包扩符号位在内的位的个数。该方法在 BigIntegers 之上实现位向量风格的集合时很有用。

isProbablePrime:如果该 BigInteger 可能是素数,则返回 true ;如果它很明确是一个合数,则返回 false 。 参数 certainty 是对调用者愿意忍受的不确定性的度量:如果该数是素数的概率超过了 1 - 1/2**certainty方法,则该方法返回 true 。执行时间正比于参数确定性的值。

compareTo:根据该数值是小于、等于、或大于 val 返回 -1、0 或 1;

equals:判断两数是否相等,也可以用compareTo来代替;

min,max:取两个数的较小、大者;

intValue,longValue,floatValue,doublue:把该数转换为该类型的数的值**

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: