最近开始刷航电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等有关),此处就贴一些其方法及常量。
BigInteger |
abs()
返回一个BigInteger,它的值是此BigInteger的绝对值。
|
BigInteger |
add(BigInteger val)
返回值为
(this + val) 。
|
BigInteger |
and(BigInteger val)
返回值为
(this & val) 。
|
BigInteger |
andNot(BigInteger val)
返回值为
(this & ~val) 。
|
int |
bitCount()
返回与其符号位不同的BigInteger的二进制补码表示中的位数。
|
int |
bitLength()
返回此BigInteger的最小二进制补码表示中的位数,
不包括符号位。
|
byte |
byteValueExact()
将此
BigInteger 转换为
byte ,检查丢失的信息。
|
BigInteger |
clearBit(int n)
返回一个BigInteger,其值等于此BigInteger,指定的位被清零。
|
int |
compareTo(BigInteger val)
将此BigInteger与指定的BigInteger进行比较。
|
BigInteger |
divide(BigInteger val)
返回值为
(this / val) 。
|
BigInteger[] |
divideAndRemainder(BigInteger val)
返回两个BigInteger的数组,其中包含
(this / val) 后跟
(this % val) 。
|
double |
doubleValue()
将此BigInteger转换为
double 。
|
boolean |
equals(Object x)
将此BigInteger与指定的对象进行比较以实现相等。
|
BigInteger |
flipBit(int n)
返回一个BigInteger,其值等于此BigInteger,指定的位被翻转。
|
float |
floatValue()
将此BigInteger转换为
float 。
|
BigInteger |
gcd(BigInteger val)
返回一个BigInteger,其值是
abs(this) 和
abs(val) 。
|
int |
getLowestSetBit()
返回此BigInteger中最右(最低位)一位的索引(最右边一位右侧的零位数)。
|
int |
hashCode()
返回此BigInteger的哈希码。
|
int |
intValue()
将此BigInteger转换为
int 。
|
int |
intValueExact()
将此
BigInteger 转换为
int ,检查丢失的信息。
|
boolean |
isProbablePrime(int certainty)
返回
true 如果这个BigInteger可能是素数,
false 如果它是绝对复合。
|
long |
longValue()
将此BigInteger转换为
long 。
|
long |
longValueExact()
将此
BigInteger 转换为
long ,检查丢失的信息。
|
BigInteger |
max(BigInteger val)
返回此BigInteger和
val 。
|
BigInteger |
min(BigInteger val)
返回此BigInteger和
val 。
|
BigInteger |
mod(BigInteger m)
返回值为
(this mod m )。
|
BigInteger |
modInverse(BigInteger m)
返回值为
(this
-1
mod m) 。
|
BigInteger |
modPow(BigInteger exponent, BigInteger m)
返回值为
(thisexponent mod m)的BigInteger 。
|
BigInteger |
multiply(BigInteger val)
返回值为
(this * val) 。
|
BigInteger |
negate()
返回值为
(-this) 。
|
BigInteger |
nextProbablePrime()
返回大于这个
BigInteger 为
BigInteger 的第一个整数。
|
BigInteger |
not()
返回值为
(~this) 。
|
BigInteger |
or(BigInteger val)
返回值为
(this | val) 。
|
BigInteger |
pow(int exponent)
返回值为
(thisexponent)的BigInteger 。
|
static BigInteger |
probablePrime(int bitLength, Random rnd)
返回一个正的BigInteger,它可能是素数,具有指定的位长度。
|
BigInteger |
remainder(BigInteger val)
返回值为
(this % val) 。
|
BigInteger |
setBit(int n)
返回一个BigInteger,其值等于具有指定位集合的BigInteger。
|
BigInteger |
shiftLeft(int n)
返回值为
(this << n) 。
|
BigInteger |
shiftRight(int n)
返回值为
(this >> n) 。
|
short |
shortValueExact()
将此
BigInteger 转换为
short ,检查丢失的信息。
|
int |
signum()
返回此BigInteger的signum函数。
|
BigInteger |
subtract(BigInteger val)
返回值为
(this - val) 。
|
boolean |
testBit(int n)
返回
true 当且仅当指定的位被设置。
|
byte[] |
toByteArray()
返回一个包含此BigInteger的二进制补码表示的字节数组。
|
String |
toString()
返回此BigInteger的十进制字符串表示形式。
|
String |
toString(int radix)
返回给定基数中BigInteger的String表示形式。
|
static BigInteger |
valueOf(long val)
返回一个BigInteger,其值等于指定的
long 。
|
BigInteger |
xor(BigInteger val)
返回值为
(this ^ val) 。
|
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();
}
}
}
}