用Java处理高精度问题

用Java处理高精度(大数?)问题

在这里插入图片描述


Java中的规范输入/输出

输入(Fuck the stupid Easyin)

import java.util.Scanner;
class InputTest {
    public static void main(String[] args) {
       	Scanner sc = new Scanner(System.in);
        	/将输入的数值存入变量中/
       	int i1 = sc.nextInt();
       	double d1 = sc.nextDouble();
       	char c1 = sc.next().charAt(0);
       	String str1 = sc.nextLine();
       	/用法很多,自行探索/
        	/使用结束后注意要将Scanner关闭/
        	sc.close();
    }
}

输出

class OutPutTest {
	public static void main(String[] args) {
        System.out.println("hello, world"); // 输出后换行
        System.out.print("hello, world"); // 输出后不换行
        System.out.printf("%s\n", "hello, world"); // 形如c语言标准输出
	}
}

两个对象(?出没注意)

BigInteger

public class BigInteger
extends Number
implements Comparable<BigInteger>

不可变的任意精度的整数。所有操作中,都以二进制补码形式表示 BigInteger(如 Java 的基本整数类型)。BigInteger 提供所有 Java 的基本整数操作符的对应物,并提供 java.lang.Math 的所有相关方法。另外,BigInteger 还提供以下运算:模算术、GCD 计算、质数测试、素数生成、位操作以及一些其他操作。

字段摘要
数据类型属性释义
static BigintegerONE常量1
static BigIntegerTEN常量10
static BigIntegerZERO常量0
构造方法摘要
方法名及参数列表释义
BigInteger (byte[] val)将包含 BigInteger 的二进制补码表示形式的 byte 数组转换为 BigInteger。
BigInteger (int signum, byte[] magnitude)将 BigInteger 的符号-数量表示形式转换为 BigInteger。
BigInteger (int bitLength, int certainty, Random rnd)构造一个随机生成的正 BigInteger,它可能是一个具有指定 bitLength 的素数。
BigInteger (int numBits, Random rnd)构造一个随机生成的 BigInteger,它是在 0(2numBits - 1)(包括)范围内均匀分布的值。
BigInteger (String val)?将 BigInteger 的十进制字符串表示形式转换为 BigInteger。
BigInteger (String val, int radix)?将指定基数的 BigInteger 的字符串表示形式转换为 BigInteger。
方法摘要
返回值类型方法名及参数列表释义
BigIntegerabs()?返回其值是此 BigInteger 的绝对值的 BigInteger。
BigIntegeradd(BigInteger val)?返回其值为 (this + val) 的 BigInteger。
BigIntegerand(BigInteger val)返回其值为 (this & val) 的 BigInteger。
BigIntegerandNot(BigInteger val)返回其值为 (this & ~val) 的 BigInteger。
intbitCount()返回此 BigInteger 的二进制补码表示形式中与符号不同的位的数量。
intbitLength()返回此 BigInteger 的最小的二进制补码表示形式的位数,不包括 符号位。
BigIntegerclearBit(int n)返回其值与清除了指定位的此 BigInteger 等效的 BigInteger。
intcompareTo(BigInteger val)?将此 BigInteger 与指定的 BigInteger 进行比较。
BigIntegerdivide(BigInteger val)?返回其值为 (this / val) 的 BigInteger。
BigInteger[]divideAndRemainder(BigInteger val)返回包含 (this / val) 后跟 (this % val) 的两个 BigInteger 的数组。
doubledoubleValue()将此 BigInteger 转换为 double
booleanequals(Object x)?比较此 BigInteger 与指定的 Object 的相等性。
BigIntegerflipBit(int n)返回其值与对此 BigInteger 进行指定位翻转后的值等效的 BigInteger。
floatfloatValue()将此 BigInteger 转换为 float
BigIntegergcd(BigInteger val)返回一个 BigInteger,其值是 abs(this)abs(val)的最大公约数。
intgetLowestSetBit()返回此 BigInteger 最右端(最低位)1 比特的索引(即从此字节的右端开始到本字节中最右端 1 比特之间的 0 比特的位数)。
inthashCode()返回此 BigInteger 的哈希码。
intintValue()将此 BigInteger 转换为 int
booleanisProbablePrime(int certainty)如果此 BigInteger 可能为素数,则返回 true,如果它一定为合数,则返回 false
longlongValue()将此 BigInteger 转换为 long
BigIntegermax(BigInteger val)?返回此 BigInteger 和 val 的最大值。
BigIntegermin(BigInteger val)?返回此 BigInteger 和 val 的最小值。
BigIntegermod(BigInteger m)?返回其值为 (this mod m) 的 BigInteger。
BigIntegermodInverse(BigInteger m)返回其值为 (this-1 mod m) 的 BigInteger。
BigIntegermodPow(BigInteger exponent, BigInteger m)?返回其值为 (thisexponent mod m) 的 BigInteger。
BigIntegermultiply(BigInteger val)?返回其值为 (this val) 的 BigInteger。
BigIntegernegate()返回其值是 (-this) 的 BigInteger。
BigIntegernextProbablePrime()返回大于此 BigInteger 的可能为素数的第一个整数。
BigIntegernot()返回其值为 (~this) 的 BigInteger。
BigIntegeror(BigInteger val)返回其值为 (this | val) 的 BigInteger。
BigIntegerpow(int exponent)?返回其值为 (thisexponent) 的 BigInteger。
static BigIntegerprobablePrime(int bitLength, Random rnd)返回有可能是素数的、具有指定长度的正 BigInteger。
BigIntegerremainder(BigInteger val)返回其值为 (this % val) 的 BigInteger。
BigIntegersetBit(int n)返回其值与设置了指定位的此 BigInteger 等效的 BigInteger。
BigIntegershiftLeft(int n)返回其值为 (this << n) 的 BigInteger。
BigIntegershiftRight(int n)返回其值为 (this >> n) 的 BigInteger。
intsignum()返回此 BigInteger 的正负号函数。
BigIntegersubtract(BigInteger val)?返回其值为 (this - val) 的 BigInteger。
booleantestBit(int n)当且仅当设置了指定的位时,返回 true
byte[]toByteArray()返回一个 byte 数组,该数组包含此 BigInteger 的二进制补码表示形式。
StringtoString()?返回此 BigInteger 的十进制字符串表示形式。
StringtoString(int radix)返回此 BigInteger 的给定基数的字符串表示形式。
static BigIntegervalueOf(long val)?返回其值等于指定 long 的值的 BigInteger。
BigIntegerxor(BigInteger val)返回其值为 (this ^ val) 的 BigInteger。

BigDecimal

public class BigDecimal
extends Number
implements Comparable<BigDecimal>

不可变的、任意精度的有符号十进制数。BigDecimal 由任意精度的整数非标度值 和 32 位的整数标度 (scale) 组成。如果为零或正数,则标度是小数点后的位数。如果为负数,则将该数的非标度值乘以 10 的负 scale 次幂。因此,BigDecimal 表示的数值是 (unscaledValue × 10-scale)

BigDecimal 类提供以下操作:算术、标度操作、舍入、比较、哈希算法和格式转换。toString() 方法提供 BigDecimal 的规范表示形式。

字段摘要
数据类型属性释义
static BigDecimalONE?值为 1,标度为 0。
static intROUND_CEILING接近正无穷大的舍入模式。
static intROUND_DOWN接近零的舍入模式。
static intROUND_FLOOR接近负无穷大的舍入模式。
static intROUND_HALF_DOWN向“最接近的”数字舍入,如果与两个相邻数字的距离相等,则为上舍入的舍入模式。
static intROUND_HALF_EVEN向“最接近的”数字舍入,如果与两个相邻数字的距离相等,则向相邻的偶数舍入。
static intROUND_HALF_UP向“最接近的”数字舍入,如果与两个相邻数字的距离相等,则为向上舍入的舍入模式。
static intROUND_UNNECESSARY断言请求的操作具有精确的结果,因此不需要舍入。
static intROUND_UP舍入远离零的舍入模式。
static BigDecimalTEN?值为 10,标度为 0。
static BigDecimalZERO?值为 0,标度为 0。
构造方法摘要
方法名及参数列表释义
BigDecimal(BigInteger val)?BigInteger 转换为 BigDecimal
BigDecimal(BigInteger unscaledVal, int scale)BigInteger 非标度值和 int 标度转换为 BigDecimal
BigDecimal(BigInteger unscaledVal, int scale, MathContext mc)BigInteger 非标度值和 int 标度转换为 BigDecimal(根据上下文设置进行舍入)。
BigDecimal(BigInteger val, MathContext mc)BigInteger 转换为 BigDecimal(根据上下文设置进行舍入)。
BigDecimal(char[] in)?BigDecimal 的字符数组表示形式转换为 BigDecimal,接受与 BigDecimal(String)构造方法相同的字符序列。
BigDecimal(char[] in, int offset, int len)BigDecimal 的字符数组表示形式转换为 BigDecimal,接受与 BigDecimal(String) 构造方法相同的字符序列,同时允许指定子数组。
BigDecimal(char[] in, int offset, int len, MathContext mc)BigDecimal 的字符数组表示形式转换为 BigDecimal,接受与 BigDecimal(String)构造方法相同的字符序列,同时允许指定子数组,并根据上下文设置进行舍入。
BigDecimal(char[] in, MathContext mc)BigDecimal 的字符数组表示形式转换为 BigDecimal,接受与 BigDecimal(String)构造方法相同的字符序列(根据上下文设置进行舍入)。
BigDecimal(double val)?double 转换为 BigDecimal,后者是 double 的二进制浮点值准确的十进制表示形式。
BigDecimal(double val, MathContext mc)double 转换为 BigDecimal(根据上下文设置进行舍入)。
BigDecimal(int val)?int 转换为 BigDecimal
BigDecimal(int val, MathContext mc)int 转换为 BigDecimal(根据上下文设置进行舍入)。
BigDecimal(long val)?long 转换为 BigDecimal
BigDecimal(long val, MathContext mc)long 转换为 BigDecimal(根据上下文设置进行舍入)。
BigDecimal(String val)?BigDecimal 的字符串表示形式转换为 BigDecimal
BigDecimal(String val, MathContext mc)BigDecimal 的字符串表示形式转换为 BigDecimal,接受与 BigDecimal(String)构造方法相同的字符串(按照上下文设置进行舍入)。
方法摘要
返回值类型方法名及参数列表释义
BigDecimalabs()?返回 BigDecimal,其值为此 BigDecimal 的绝对值,其标度为 this.scale()
BigDecimalabs(MathContext mc)返回其值为此 BigDecimal 绝对值的 BigDecimal(根据上下文设置进行舍入)。
BigDecimaladd(BigDecimal augend)?返回一个 BigDecimal,其值为 (this + augend),其标度为 max(this.scale(), augend.scale())
BigDecimaladd(BigDecimal augend, MathContext mc)返回其值为 (this + augend)BigDecimal(根据上下文设置进行舍入)。
bytebyteValueExact()将此 BigDecimal 转换为 byte,以检查丢失的信息。
intcompareTo(BigDecimal val)?将此 BigDecimal 与指定的 BigDecimal 比较。
BigDecimaldivide(BigDecimal divisor)?返回一个 BigDecimal,其值为 (this / divisor),其首选标度为 (this.scale() - divisor.scale());如果无法表示准确的商值(因为它有无穷的十进制扩展),则抛出 ArithmeticException
BigDecimaldivide(BigDecimal divisor, int roundingMode)返回一个 BigDecimal,其值为 (this / divisor),其标度为 this.scale()
BigDecimaldivide(BigDecimal divisor, int scale, int roundingMode)返回一个 BigDecimal,其值为 (this / divisor),其标度为指定标度。
BigDecimaldivide(BigDecimal divisor, int scale, RoundingMode roundingMode)返回一个 BigDecimal,其值为 (this / divisor),其标度为指定标度。
BigDecimaldivide(BigDecimal divisor, MathContext mc)返回其值为 (this / divisor)BigDecimal(根据上下文设置进行舍入)。
BigDecimaldivide(BigDecimal divisor, RoundingMode roundingMode)返回一个 BigDecimal,其值为 (this / divisor),其标度为 this.scale()
BigDecimal[]divideAndRemainder(BigDecimal divisor)返回由两个元素组成的 BigDecimal 数组,该数组包含 divideToIntegralValue 的结果,后跟对两个操作数计算所得到的 remainder
BigDecimal[]divideAndRemainder(BigDecimal divisor, MathContext mc)返回由两个元素组成的 BigDecimal 数组,该数组包含 divideToIntegralValue 的结果,后跟根据上下文设置对两个操作数进行舍入计算所得到的 remainder 的结果。
BigDecimaldivideToIntegralValue(BigDecimal divisor)返回 BigDecimal,其值为向下舍入所得商值 (this / divisor) 的整数部分。
BigDecimaldivideToIntegralValue(BigDecimal divisor, MathContext mc)返回 BigDecimal,其值为 (this / divisor) 的整数部分。
doubledoubleValue()将此 BigDecimal 转换为 double
booleanequals(Object x)?比较此 BigDecimal 与指定的 Object 的相等性。
floatfloatValue()将此 BigDecimal 转换为 float
inthashCode()返回此 BigDecimal 的哈希码。
intintValue()将此 BigDecimal 转换为 int
intintValueExact()将此 BigDecimal 转换为 int,以检查丢失的信息。
longlongValue()将此 BigDecimal 转换为 long
longlongValueExact()将此 BigDecimal 转换为 long,以检查丢失的信息。
BigDecimalmax(BigDecimal val)?返回此 BigDecimalval 的最大值。
BigDecimalmin(BigDecimal val):feet:返回此 BigDecimalval 的最小值。
BigDecimalmovePointLeft(int n)返回一个 BigDecimal,它等效于将该值的小数点向左移动 n 位。
BigDecimalmovePointRight(int n)返回一个 BigDecimal,它等效于将该值的小数点向右移动 n 位。
BigDecimalmultiply(BigDecimal multiplicand)?返回一个 BigDecimal,其值为 (this × multiplicand),其标度为 (this.scale() + multiplicand.scale())
BigDecimalmultiply(BigDecimal multiplicand, MathContext mc)返回其值为 (this × multiplicand)BigDecimal(根据上下文设置进行舍入)。
BigDecimalnegate()返回 BigDecimal,其值为 (-this),其标度为 this.scale()
BigDecimalnegate(MathContext mc)返回其值为 (-this)BigDecimal(根据上下文设置进行舍入)。
BigDecimalplus()返回 BigDecimal,其值为 (+this),其标度为 this.scale()
BigDecimalplus(MathContext mc)返回其值为 (+this)BigDecimal(根据上下文设置进行舍入)。
BigDecimalpow(int n)?返回其值为 (thisn)BigDecimal,准确计算该幂,使其具有无限精度。
BigDecimalpow(int n, MathContext mc)返回其值为 (thisn)BigDecimal
intprecision()返回此 BigDecimal 的精度。
BigDecimalremainder(BigDecimal divisor)返回其值为 (this % divisor)BigDecimal
BigDecimalremainder(BigDecimal divisor, MathContext mc)返回其值为 (this % divisor)BigDecimal(根据上下文设置进行舍入)。
BigDecimalround(MathContext mc)返回根据 MathContext 设置进行舍入后的 BigDecimal
intscale()返回此 BigDecimal 的标度。
BigDecimalscaleByPowerOfTen(int n)返回其数值等于 (this 10n) 的 BigDecimal。
BigDecimalsetScale(int newScale)返回一个 BigDecimal,其标度为指定值,其值在数值上等于此 BigDecimal 的值。
BigDecimalsetScale(int newScale, int roundingMode)返回一个 BigDecimal,其标度为指定值,其非标度值通过此 BigDecimal 的非标度值乘以或除以十的适当次幂来确定,以维护其总值。
BigDecimalsetScale(int newScale, RoundingMode roundingMode)返回 BigDecimal,其标度为指定值,其非标度值通过此 BigDecimal 的非标度值乘以或除以十的适当次幂来确定,以维护其总值。
shortshortValueExact()将此 BigDecimal 转换为 short,以检查丢失的信息。
intsignum()返回此 BigDecimal 的正负号函数。
BigDecimalstripTrailingZeros()返回数值上等于此小数,但从该表示形式移除所有尾部零的 BigDecimal
BigDecimalsubtract(BigDecimal subtrahend)返回一个 BigDecimal,其值为 (this - subtrahend),其标度为 max(this.scale(), subtrahend.scale())
BigDecimalsubtract(BigDecimal subtrahend, MathContext mc)返回其值为 (this - subtrahend)BigDecimal(根据上下文设置进行舍入)。
BigIntegertoBigInteger()将此 BigDecimal 转换为 BigInteger
BigIntegertoBigIntegerExact()将此 BigDecimal 转换为 BigInteger,以检查丢失的信息。
StringtoEngineeringString()返回此 BigDecimal 的字符串表示形式,需要指数时,则使用工程计数法。
StringtoPlainString()??返回不带指数字段的此 BigDecimal 的字符串表示形式。
StringtoString()?返回此 BigDecimal 的字符串表示形式,如果需要指数,则使用科学记数法。
BigDecimalulp()返回此 BigDecimal 的 ulp(最后一位的单位)的大小。
BigIntegerunscaledValue()返回其值为此 BigDecimal 的非标度值 的 BigInteger
static BigDecimalvalueOf(double val)使用 Double.toString(double)方法提供的 double 规范的字符串表示形式将 double 转换为 BigDecimal
static BigDecimalvalueOf(long val)long 值转换为具有零标度的 BigDecimal
static BigDecimalvalueOf(long unscaledVal, int scale)long 非标度值和 int 标度转换为 BigDecimal

几个例子

大数A+B

代码
import java.math.BigInteger;
import java.util.Scanner;

class BigNumberTest {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        BigInteger bI1 = sc.nextBigInteger(),
                bI2 = sc.nextBigInteger(),
                bI3 = bI1.add(bI2);
        System.out.println(bI3);
        sc.close();
    }
}
对比一下cpp大数模板
#include<iostream> 
#include<string> 
#include<iomanip> 
#include<algorithm> 
using namespace std; 

#define MAXN 9999
#define MAXSIZE 10
#define DLEN 4

class BigNum
{ 
private: 
	int a[500];    //可以控制大数的位数 
	int len;       //大数长度
public: 
	BigNum(){ len = 1;memset(a,0,sizeof(a)); }   //构造函数
	BigNum(const int);       //将一个int类型的变量转化为大数
	BigNum(const char*);     //将一个字符串类型的变量转化为大数
	BigNum(const BigNum &);  //拷贝构造函数
	BigNum &operator=(const BigNum &);   //重载赋值运算符,大数之间进行赋值运算

	friend istream& operator>>(istream&,  BigNum&);   //重载输入运算符
	friend ostream& operator<<(ostream&,  BigNum&);   //重载输出运算符

	BigNum operator+(const BigNum &) const;   //重载加法运算符,两个大数之间的相加运算 
	BigNum operator-(const BigNum &) const;   //重载减法运算符,两个大数之间的相减运算 
	BigNum operator*(const BigNum &) const;   //重载乘法运算符,两个大数之间的相乘运算 
	BigNum operator/(const int   &) const;    //重载除法运算符,大数对一个整数进行相除运算

	BigNum operator^(const int  &) const;    //大数的n次方运算
	int    operator%(const int  &) const;    //大数对一个int类型的变量进行取模运算    
	bool   operator>(const BigNum & T)const;   //大数和另一个大数的大小比较
	bool   operator>(const int & t)const;      //大数和一个int类型的变量的大小比较

	void print();       //输出大数
}; 
BigNum::BigNum(const int b)     //将一个int类型的变量转化为大数
{ 
	int c,d = b;
	len = 0;
	memset(a,0,sizeof(a));
	while(d > MAXN)
	{
		c = d - (d / (MAXN + 1)) * (MAXN + 1); 
		d = d / (MAXN + 1);
		a[len++] = c;
	}
	a[len++] = d;
}
BigNum::BigNum(const char*s)     //将一个字符串类型的变量转化为大数
{
	int t,k,index,l,i;
	memset(a,0,sizeof(a));
	l=strlen(s);   
	len=l/DLEN;
	if(l%DLEN)
		len++;
	index=0;
	for(i=l-1;i>=0;i-=DLEN)
	{
		t=0;
		k=i-DLEN+1;
		if(k<0)
			k=0;
		for(int j=k;j<=i;j++)
			t=t*10+s[j]-'0';
		a[index++]=t;
	}
}
BigNum::BigNum(const BigNum & T) : len(T.len)  //拷贝构造函数
{ 
	int i; 
	memset(a,0,sizeof(a)); 
	for(i = 0 ; i < len ; i++)
		a[i] = T.a[i]; 
} 
BigNum & BigNum::operator=(const BigNum & n)   //重载赋值运算符,大数之间进行赋值运算
{
	int i;
	len = n.len;
	memset(a,0,sizeof(a)); 
	for(i = 0 ; i < len ; i++) 
		a[i] = n.a[i]; 
	return *this; 
}
istream& operator>>(istream & in,  BigNum & b)   //重载输入运算符
{
	char ch[MAXSIZE*4];
	int i = -1;
	in>>ch;
	int l=strlen(ch);
	int count=0,sum=0;
	for(i=l-1;i>=0;)
	{
		sum = 0;
		int t=1;
		for(int j=0;j<4&&i>=0;j++,i--,t*=10)
		{
			sum+=(ch[i]-'0')*t;
		}
		b.a[count]=sum;
		count++;
	}
	b.len =count++;
	return in;

}
ostream& operator<<(ostream& out,  BigNum& b)   //重载输出运算符
{
	int i;  
	cout << b.a[b.len - 1]; 
	for(i = b.len - 2 ; i >= 0 ; i--)
	{ 
		cout.width(DLEN); 
		cout.fill('0'); 
		cout << b.a[i]; 
	} 
	return out;
}

BigNum BigNum::operator+(const BigNum & T) const   //两个大数之间的相加运算
{
	BigNum t(*this);
	int i,big;      //位数   
	big = T.len > len ? T.len : len; 
	for(i = 0 ; i < big ; i++) 
	{ 
		t.a[i] +=T.a[i]; 
		if(t.a[i] > MAXN) 
		{ 
			t.a[i + 1]++; 
			t.a[i] -=MAXN+1; 
		} 
	} 
	if(t.a[big] != 0)
		t.len = big + 1; 
	else
		t.len = big;   
	return t;
}
BigNum BigNum::operator-(const BigNum & T) const   //两个大数之间的相减运算 
{  
	int i,j,big;
	bool flag;
	BigNum t1,t2;
	if(*this>T)
	{
		t1=*this;
		t2=T;
		flag=0;
	}
	else
	{
		t1=T;
		t2=*this;
		flag=1;
	}
	big=t1.len;
	for(i = 0 ; i < big ; i++)
	{
		if(t1.a[i] < t2.a[i])
		{ 
			j = i + 1; 
			while(t1.a[j] == 0)
				j++; 
			t1.a[j--]--; 
			while(j > i)
				t1.a[j--] += MAXN;
			t1.a[i] += MAXN + 1 - t2.a[i]; 
		} 
		else
			t1.a[i] -= t2.a[i];
	}
	t1.len = big;
	while(t1.a[t1.len - 1] == 0 && t1.len > 1)
	{
		t1.len--; 
		big--;
	}
	if(flag)
		t1.a[big-1]=0-t1.a[big-1];
	return t1; 
} 

BigNum BigNum::operator*(const BigNum & T) const   //两个大数之间的相乘运算 
{ 
	BigNum ret; 
	int i,j,up; 
	int temp,temp1;   
	for(i = 0 ; i < len ; i++)
	{ 
		up = 0; 
		for(j = 0 ; j < T.len ; j++)
		{ 
			temp = a[i] * T.a[j] + ret.a[i + j] + up; 
			if(temp > MAXN)
			{ 
				temp1 = temp - temp / (MAXN + 1) * (MAXN + 1); 
				up = temp / (MAXN + 1); 
				ret.a[i + j] = temp1; 
			} 
			else
			{ 
				up = 0; 
				ret.a[i + j] = temp; 
			} 
		} 
		if(up != 0) 
			ret.a[i + j] = up; 
	} 
	ret.len = i + j; 
	while(ret.a[ret.len - 1] == 0 && ret.len > 1)
		ret.len--; 
	return ret; 
} 
BigNum BigNum::operator/(const int & b) const   //大数对一个整数进行相除运算
{ 
	BigNum ret; 
	int i,down = 0;   
	for(i = len - 1 ; i >= 0 ; i--)
	{ 
		ret.a[i] = (a[i] + down * (MAXN + 1)) / b; 
		down = a[i] + down * (MAXN + 1) - ret.a[i] * b; 
	} 
	ret.len = len; 
	while(ret.a[ret.len - 1] == 0 && ret.len > 1)
		ret.len--; 
	return ret; 
}
int BigNum::operator %(const int & b) const    //大数对一个int类型的变量进行取模运算    
{
	int i,d=0;
	for (i = len-1; i>=0; i--)
	{
		d = ((d * (MAXN+1))% b + a[i])% b;  
	}
	return d;
}
BigNum BigNum::operator^(const int & n) const    //大数的n次方运算
{
	BigNum t,ret(1);
	int i;
	if(n<0)
		exit(-1);
	if(n==0)
		return 1;
	if(n==1)
		return *this;
	int m=n;
	while(m>1)
	{
		t=*this;
		for( i=1;i<<1<=m;i<<=1)
		{
			t=t*t;
		}
		m-=i;
		ret=ret*t;
		if(m==1)
			ret=ret*(*this);
	}
	return ret;
}
bool BigNum::operator>(const BigNum & T) const   //大数和另一个大数的大小比较
{ 
	int ln; 
	if(len > T.len)
		return true; 
	else if(len == T.len)
	{ 
		ln = len - 1; 
		while(a[ln] == T.a[ln] && ln >= 0)
			ln--; 
		if(ln >= 0 && a[ln] > T.a[ln])
			return true; 
		else
			return false; 
	} 
	else
		return false; 
}
bool BigNum::operator >(const int & t) const    //大数和一个int类型的变量的大小比较
{
	BigNum b(t);
	return *this>b;
}

void BigNum::print()    //输出大数
{ 
	int i;   
	cout << a[len - 1]; 
	for(i = len - 2 ; i >= 0 ; i--)
	{ 
		cout.width(DLEN); 
		cout.fill('0'); 
		cout << a[i]; 
	} 
	cout << endl;
}

Java将大数类封装在Biginteger这个package里,从而降低了程序员工作的难度,可见语言创始人考虑周到的重要性?


ZCMU1179: ab-ba

题解
import java.math.BigInteger;
import java.util.Scanner;

import static java.lang.System.*;

class BigNumberTest {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        BigInteger bI1 = sc.nextBigInteger(),
                bI2 = sc.nextBigInteger(),
                ans = bI1.pow(bI2.intValue()).subtract(bI2.pow(bI1.intValue()));
        out.println(ans);
        sc.close();
    }
}

A+B

题目描述

给定两个整数A和B,其表示形式是:从个位开始,每三位数用逗号","隔开。 现在请计算A+B的结果,并以正常形式输出。

输入描述:

输入包含多组数据数据,每组数据占一行,由两个整数A和B组成(-10^9 < A,B < 10^9)。

输出描述:

请计算A+B的结果,并以正常形式输出,每组数据占一行。

示例1

输入

-234,567,890 123,456,789
1,234 2,345,678

输出

-111111101
2346912

题目链接

题解
import java.math.BigInteger;
import java.util.Scanner;

import static java.lang.System.*;

class BigNumberTest {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String tmp;
        BigInteger bI1, bI2, ans;
        while (sc.hasNext()) {
            tmp = sc.nextLine();
            String[] str = tmp.split(" ", 2);
            str[0] = str[0].replaceAll(",", "");
            bI1 = new BigInteger(str[0]);
            str[1] = str[1].replaceAll(",", "");
            bI2 = new BigInteger(str[1]);
            ans = bI1.add(bI2);
            out.println(ans);
        }
        sc.close();
    }
}


使用Java 答题时的注意事项

  • 提交的代码主方法所在的类名必须为public class Main{},否则判题机不识别

  • 尽量使用IDE或带有代码高亮以及自动格式化代码功能的编辑器进行编写,没有的同学也可以选择在云IDE上进行在线编译及测试

    下载地址

    Java IDE: Intellij Idea(强烈推荐​JetBrains全家桶​,一桶在手,编译无忧?), Eclipse

    编辑器:Visual Studio Code, sublime text 3

    云IDE: www.ideone.com, www.compilejava.net

  • 程序员最好的老师永远是文档,去网上找找Java的API可发现更多更好玩的秘密?

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值