java 之 大整数

来源:互联网 发布:java浏览器调用ie内核 编辑:程序博客网 时间:2024/06/02 13:25

愿所有比赛没有语言优势,哈哈,谢谢阳哥去年给了我一本书




poj 1503

Input

The input will consist of at most 100 lines of text, each of which contains a single VeryLongInteger. Each VeryLongInteger will be 100 or fewer characters in length, and will only contain digits (no VeryLongInteger will be negative). 

The final input line will contain a single zero on a line by itself. 

Output

Your program should output the sum of the VeryLongIntegers given in the input.

Sample Input

1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678900

Sample Output

370370367037037036703703703670

import java.io.*;import java.util.*;import java.math.*;public class Main{    public static void main(String[] args)    {        String str;        BigDecimal a,b;        Scanner cin=new Scanner(System.in);        b=BigDecimal.valueOf(0);        while(cin.hasNext())        {        str=cin.nextLine();        a=new BigDecimal(str);            if(str.equals("0"))            break;            b=b.add(a);        }        System.out.println(b);    }}


比较大小

import java.io.*;import java.util.*;import java.math.*;       public class Main {         public static void main(String[] args) {          Scanner cin = new Scanner(System.in);          int cases = 1;          while(cin.hasNext()){              String str = cin.next();              String str1 = cin.next();              BigDecimal a = new BigDecimal(str);              BigDecimal  b = new BigDecimal(str1);              System.out.print("Case "+(cases++)+": ");              if(a.compareTo(b)==0){                  System.out.println("Same");              }else if(a.compareTo(b)<0){                  System.out.println("Smaller");              }else{                  System.out.println("Bigger");              }          }      }  }  




poj 1001 

Sample Input

95.123 120.4321 205.1234 156.7592  998.999 101.0100 12

Sample Output

548815620517731830194541.899025343415715973535967221869852721.0000000514855464107695612199451127676715483848176020072635120383542976301346240143992025569.92857370126648804114665499331870370751166629547672049395302429448126.76412102161816443020690903717327667290429072743629540498.1075960194566517745610440100011.126825030131969720661201




import java.util.*;  import java.math.*;  import java.io.*;    public class Main {      public static void main(String args[])      {      int num;    String str,temp;    BigDecimal a,b;        Scanner cin = new Scanner(System.in);          while(cin.hasNext())          {          temp = cin.next();              num = cin.nextInt();              a = new BigDecimal(temp);                        a = a.pow(num);                        a=a.stripTrailingZeros();            str = a.toPlainString();              if(str.startsWith("0."))            str=str.substring(1);                        System.out.println(str);          }      }  }  


去掉尾部  0

a=a.stripTrailingZeros();
把科学计数法变成普通数

 str = a.toPlainString();  
找前面是不是0.000541开头的

str.startsWith("0.")
截取子串,从第二个字符开始

str=str.substring(1);




下面是转载别人的


   

方法摘要

 BigInteger

abs()
          返回其值是此 BigInteger 的绝对值的 BigInteger

 BigInteger

add(BigInteger val)
          返回其值为 (this val) 的 BigInteger

 BigInteger

and(BigInteger val)
          返回其值为 (this val) 的 BigInteger

 BigInteger

andNot(BigInteger val)
          返回其值为 (this ~val) 的 BigInteger

 int

bitCount()
          返回此 BigInteger 的二进制补码表示形式中与符号不同的位的数量。

 int

bitLength()
          返回此 BigInteger 的最小的二进制补码表示形式的位数,不包括 符号位。

 BigInteger

clearBit(int n)
          返回其值与清除了指定位的此 BigInteger 等效的 BigInteger

 int

compareTo(BigInteger val)
          将此 BigInteger 与指定的 BigInteger 进行比较。

 BigInteger

divide(BigInteger val)
          返回其值为 (this val) 的 BigInteger

 BigInteger[]

divideAndRemainder(BigInteger val)
          返回包含 (this val) 后跟 (this val) 的两个 BigInteger 的数组。

 double

doubleValue()
          将此 BigInteger 转换为 double

 boolean

equals(Object x)
          比较此 BigInteger 与指定的 Object 的相等性。

 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

 boolean

isProbablePrime(int certainty)
          如果此 BigInteger 可能为素数,则返回 true,如果它一定为合数,则返回 false

 long

longValue()
          将此 BigInteger 转换为 long

 BigInteger

max(BigInteger val)
          返回此 BigInteger 和 val 的最大值。

 BigInteger

min(BigInteger val)
          返回此 BigInteger 和 val 的最小值。

 BigInteger

mod(BigInteger m)
          返回其值为 (this mod m) 的 BigInteger

 BigInteger

modInverse(BigInteger m)
          返回其值为 (this-1 mod m) 的 BigInteger

 BigInteger

modPow(BigInteger exponent, BigInteger m)
          返回其值为 (thisexponent mod m) 的 BigInteger

 BigInteger

multiply(BigInteger val)
          返回其值为 (this val) 的 BigInteger

 BigInteger

negate()
          返回其值是 (-this) 的 BigInteger

 BigInteger

nextProbablePrime()
          返回大于此 BigInteger 的可能为素数的第一个整数。

 BigInteger

not()
          返回其值为 (~this) 的 BigInteger

 BigInteger

or(BigInteger val)
          返回其值为 (this val) 的 BigInteger

 BigInteger

pow(int exponent)
          返回其值为 (thisexponent的 BigInteger

static BigInteger

probablePrime(int bitLength, Random rnd)
          返回有可能是素数的、具有指定长度的正 BigInteger

 BigInteger

remainder(BigInteger val)
          返回其值为 (this val) 的 BigInteger

 BigInteger

setBit(int n)
          返回其值与设置了指定位的此 BigInteger 等效的 BigInteger

 BigInteger

shiftLeft(int n)
          返回其值为 (this << n) 的 BigInteger

 BigInteger

shiftRight(int n)
          返回其值为 (this >> n) 的 BigInteger

 int

signum()
          返回此 BigInteger 的正负号函数。

 BigInteger

subtract(BigInteger val)
          返回其值为 (this val) 的 BigInteger

 boolean

testBit(int n)
          当且仅当设置了指定的位时,返回 true

 byte[]

toByteArray()
          返回一个 byte 数组,该数组包含此 BigInteger 的二进制补码表示形式。

 String

toString()
          返回此 BigInteger 的十进制字符串表示形式。

 String

toString(int radix)
          返回此 BigInteger 的给定基数的字符串表示形式。

static BigInteger

valueOf(long val)
          返回其值等于指定 long 的值的 BigInteger

 BigInteger

xor(BigInteger val)
          返回其值为 (this val) 的 BigInteger


0 0
原创粉丝点击