BigInteger浅谈

最近开始刷航电OJ。。其中一道这样的题目

首先无力的吐槽一下这道题真的。。,在讨论区看到用JAVA必须以另一种格式输出(与题目要求不一样,而且还简单。。费了老大劲居然因为这个没AC),所以也不要吐槽下面我的代码的输出格式了。

好了就不多扯淡了。。。回归正题,这是我第一次的代码:

import java.util.Scanner;
public class Main {
        public static void main(String args[]) {
            Scanner scan=new Scanner(System.in);
            int i=scan.nextInt();
            long sum=0;
            int z=0;
            while(z<i) {
                long a=scan.nextInt();
                long b=scan.nextInt();
                sum=a+b;
                System.out.println("Case "+(z+1)+":");
                System.out.println(a+" + "+b+" = "+sum);
                z++;
                if(z<i) {
                    System.out.println();
                }
            }
        
}
}
但是一直WA。。也是费了大劲儿找到了用JAVA AC的实例,对比了一下问题出在使用long声明两个范围假定很大的数,特别看了一下题目声明,然后查了一查BigInteger和其他整型,首先普遍一下基础知识。

byte的取值范围为-128~127,占用1个字节(-2的7次方到2的7次方-1)
short的取值范围为-32768~32767,占用2个字节(-2的15次方到2的15次方-1)
int的取值范围为(-2147483648~2147483647),占用4个字节(-2的31次方到2的31次方-1)
long的取值范围为(-9223372036854774808~9223372036854774807),占用8个字节(-2的63次方到2的63次方-1)
此处推荐你用xxx.MAX_VALUE输出看一下,每个类型都有这个常量。
关于BigInteger,我也查了一下JDK文档,理论上是无穷大的(与内存及CPU等有关),此处就贴一些其方法及常量。
BigIntegerabs()
返回一个BigInteger,它的值是此BigInteger的绝对值。
BigIntegeradd(BigInteger val)
返回值为 (this + val)
BigIntegerand(BigInteger val)
返回值为 (this & val)
BigIntegerandNot(BigInteger val)
返回值为 (this & ~val)
intbitCount()
返回与其符号位不同的BigInteger的二进制补码表示中的位数。
intbitLength()
返回此BigInteger的最小二进制补码表示中的位数, 包括符号位。
bytebyteValueExact()
将此 BigInteger转换为 byte ,检查丢失的信息。
BigIntegerclearBit(int n)
返回一个BigInteger,其值等于此BigInteger,指定的位被清零。
intcompareTo(BigInteger val)
将此BigInteger与指定的BigInteger进行比较。
BigIntegerdivide(BigInteger val)
返回值为 (this / val)
BigInteger[]divideAndRemainder(BigInteger val)
返回两个BigInteger的数组,其中包含 (this / val)后跟 (this % val)
doubledoubleValue()
将此BigInteger转换为 double
booleanequals(Object x)
将此BigInteger与指定的对象进行比较以实现相等。
BigIntegerflipBit(int n)
返回一个BigInteger,其值等于此BigInteger,指定的位被翻转。
floatfloatValue()
将此BigInteger转换为 float
BigIntegergcd(BigInteger val)
返回一个BigInteger,其值是 abs(this)abs(val)
intgetLowestSetBit()
返回此BigInteger中最右(最低位)一位的索引(最右边一位右侧的零位数)。
inthashCode()
返回此BigInteger的哈希码。
intintValue()
将此BigInteger转换为 int
intintValueExact()
将此 BigInteger转换为 int ,检查丢失的信息。
booleanisProbablePrime(int certainty)
返回 true如果这个BigInteger可能是素数, false如果它是绝对复合。
longlongValue()
将此BigInteger转换为 long
longlongValueExact()
将此 BigInteger转换为 long ,检查丢失的信息。
BigIntegermax(BigInteger val)
返回此BigInteger和 val
BigIntegermin(BigInteger val)
返回此BigInteger和 val
BigIntegermod(BigInteger m)
返回值为 (this mod m )。
BigIntegermodInverse(BigInteger m)
返回值为 (this -1 mod m)
BigIntegermodPow(BigInteger exponent, BigInteger m)
返回值为 (thisexponent mod m)的BigInteger
BigIntegermultiply(BigInteger val)
返回值为 (this * val)
BigIntegernegate()
返回值为 (-this)
BigIntegernextProbablePrime()
返回大于这个 BigIntegerBigInteger的第一个整数。
BigIntegernot()
返回值为 (~this)
BigIntegeror(BigInteger val)
返回值为 (this | val)
BigIntegerpow(int exponent)
返回值为 (thisexponent)的BigInteger
static BigIntegerprobablePrime(int bitLength, Random rnd)
返回一个正的BigInteger,它可能是素数,具有指定的位长度。
BigIntegerremainder(BigInteger val)
返回值为 (this % val)
BigIntegersetBit(int n)
返回一个BigInteger,其值等于具有指定位集合的BigInteger。
BigIntegershiftLeft(int n)
返回值为 (this << n)
BigIntegershiftRight(int n)
返回值为 (this >> n)
shortshortValueExact()
将此 BigInteger转换为 short ,检查丢失的信息。
intsignum()
返回此BigInteger的signum函数。
BigIntegersubtract(BigInteger val)
返回值为 (this - val)
booleantestBit(int n)
返回 true当且仅当指定的位被设置。
byte[]toByteArray()
返回一个包含此BigInteger的二进制补码表示的字节数组。
StringtoString()
返回此BigInteger的十进制字符串表示形式。
StringtoString(int radix)
返回给定基数中BigInteger的String表示形式。
static BigIntegervalueOf(long val)
返回一个BigInteger,其值等于指定的 long
BigIntegerxor(BigInteger val)
返回值为 (this ^ val)
最后附上笔者的成功AC源码(可以参照一下其构造方法)
 
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
        public static void main(String args[]) {
            Scanner scan=new Scanner(System.in);
            int i=scan.nextInt();
            BigInteger sum;
            int z=0;
            while(z<i) {
                BigInteger a=new BigInteger(scan.next());
                BigInteger b=new BigInteger(scan.next());
                sum=a.add(b);
                System.out.println("Case "+(z+1)+":");
                System.out.println(a+" + "+b+" = "+sum);
                z++;
                if(z<i) {
                    System.out.println();
                }
            }
        
}
}




 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值