高精度

JAVA版(https://blog.csdn.net/piaocoder/article/details/47071935)

在用C或者C++处理大数时感觉非常麻烦,但是在JAVA中有两个类BigInteger和BigDecimal分别表示大整数类和大浮点数类,至于两个类的对象能表示最大范围不清楚,理论上能够表示无线大的数,只要计算机内存足够大。
这两个类都在java.math.*包中,因此每次必须在开头处引用该包。

Ⅰ基本函数:

1.valueOf(parament); 将参数转换为制定的类型

比如 int a=3;

BigInteger b=BigInteger.valueOf(a);
则b=3;
String s=”12345”;
BigInteger c=BigInteger.valueOf(s);

则c=12345;

 

2.add(); 大整数相加

BigInteger a=new BigInteger(“23”);
BigInteger b=new BigInteger(“34”);

a.add(b);

 

3.subtract(); 相减

4.multiply(); 相乘

5.divide();    相除取整

6.remainder(); 取余

7.pow();   a.pow(b)=a^b

8.gcd();   最大公约数

9.abs(); 绝对值

10.negate(); 取反数

11.mod(); a.mod(b)=a%b=a.remainder(b);

12.max(); min();

13.public int compareTo();

14.boolean equals(); 是否相等

15.BigInteger构造函数:
一般用到以下两种:
BigInteger(String val);
将指定字符串转换为十进制表示形式;
BigInteger(String val,int radix);
将指定基数的 BigInteger 的字符串表示形式转换为 BigInteger

Ⅱ.基本常量:

A=BigInteger.ONE    1

B=BigInteger.TEN    10

C=BigInteger.ZERO   0

Ⅲ.基本操作

1.读入:

用Scanner类定义对象进行控制台读入,Scanner类在java.util.*包中

[java] view plain copy
 
  1. Scanner cin=new Scanner(System.in);// 读入  
  2. while(cin.hasNext)   //等同于!=EOF  
  3. {  
  4.  int n;  
  5.  BigInteger m;  
  6.  n=cin.nextInt(); //读入一个int;  
  7.  m=cin.nextBigInteger();//读入一个BigInteger;  
  8.  System.out.print(m);  
  9. }  

Ⅳ.运用

四则预算:

 

[java] view plain copy
 
  1. import java.math.BigDecimal;  
  2. import java.math.BigInteger;  
  3. import java.text.DecimalFormat;  
  4. import java.util.Scanner;  
  5. public class Main    
  6. {    
  7.     public static void main(String args[])    
  8.     {  
  9.         Scanner sca = new Scanner(System.in);  
  10.         BigInteger a,b;  
  11.         int c;//为小数设置精度  
  12.         char op;//运算符  
  13.         String s;  
  14.         while(sca.hasNext()){  
  15.             a = sca.nextBigInteger();  
  16.             s = sca.next();  
  17.             op = s.charAt(0);  
  18.             b = sca.nextBigInteger();  
  19.             if(op == '+')  
  20.                 System.out.println(a.add(b));  
  21.             else if(op == '-')  
  22.                 System.out.println(a.subtract(b));  
  23.             else if(op == '*')  
  24.                 System.out.println(a.multiply(b));  
  25.             else{  
  26.                 BigDecimal t1,t2,eps;  
  27.                 String s1,s2,temp;  
  28.                 s1 = a.toString();  
  29.                 s2 = b.toString();  
  30.                 t1 = new BigDecimal(s1);  
  31.                 t2 = new BigDecimal(s2);  
  32.                 c = sca.nextInt();  
  33.                 eps = t1.divide(t2,c,4);  
  34.                 //System.out.println(a + " " + b + " " + c);    
  35.                 //System.out.println(t1.doubleValue() + " " + t2.doubleValue() + " " + c);  
  36.                 System.out.print(a.divide(b) +" "+ a.mod(b)+" ");  
  37.                 if(c != 0){  
  38.                     temp = "0.";  
  39.                     for(int i = 0; i < c; i++)  
  40.                         temp += "0";  
  41.                     DecimalFormat gd = new DecimalFormat(temp);  
  42.                     System.out.println(gd.format(eps));  
  43.                 }  
  44.                 else  
  45.                     System.out.println(eps);  
  46.             }  
  47.         }  
  48.     }  
  49. }  
  50.     
  51. //=====================================================================================//    

charAt
public char charAt(int index)
Returns the char value at the specified index. An index ranges from 0 to length() - 1. The first char value of the sequence is at index 0, the next at index 1, and so on, as for array indexing. 
If the char value specified by the index is a surrogate, the surrogate value is returned.

 

 

divide
public BigDecimal divide(BigDecimal divisor,int scale,int roundingMode)
Returns a BigDecimal whose value is (this / divisor), and whose scale is as specified. If rounding must be performed to generate a result with the specified scale, the specified rounding mode is applied. 

 

public DecimalFormat(String pattern)
Creates a DecimalFormat using the given pattern and the symbols for the default locale. This is a convenient way to obtain a DecimalFormat when internationalization is not the main concern.
To obtain standard formats for a given locale, use the factory methods on NumberFormat such as getNumberInstance. These factories will return the most appropriate sub-class of NumberFormat for a given locale.

hdu1002

http://acm.hdu.edu.cn/showproblem.php?pid=1002

[java] view plain copy
 
  1. import java.math.BigInteger;  
  2. import java.util.Scanner;  
  3.   
  4. public class Main {  
  5.     public static void main(String[] args) {  
  6.         Scanner sca = new Scanner(System.in);  
  7.         int T = sca.nextInt(),t = 1;  
  8.         while(t <= T){  
  9.             BigInteger a,b;  
  10.             a = sca.nextBigInteger();  
  11.             b = sca.nextBigInteger();  
  12.             BigInteger c = a.add(b);  
  13.             System.out.println("Case "+ t + ":");  
  14.             System.out.println(a + " + " + b + " = " + c);  
  15.             if(t != T)  
  16.                 System.out.println("");  
  17.             t++;  
  18.         }  
  19.     }  
  20. }  

 


hdu1042

 

http://acm.hdu.edu.cn/showproblem.php?pid=1042

 

[java] view plain copy
 
  1. import java.math.BigInteger;  
  2. import java.util.Scanner;  
  3.   
  4. public class Main {  
  5.     public static void main(String[] args) {  
  6.         Scanner sca = new Scanner(System.in);    
  7.         BigInteger a[] = new BigInteger[10005];  
  8.         a[0] = BigInteger.ONE;  
  9.         for(int i = 1; i <= 10000; i++)  
  10.             a[i] = a[i - 1].multiply(BigInteger.valueOf(i));  
  11.         int n;  
  12.         while(sca.hasNextInt()){  
  13.             n = sca.nextInt();  
  14.             System.out.println(a[n]);  
  15.         }  
  16.     }  
  17. }  


hdu1047

 

http://acm.hdu.edu.cn/showproblem.php?pid=1047

[java] view plain copy
 
  1. import java.math.BigInteger;  
  2. import java.util.Scanner;  
  3.   
  4. public class Main {  
  5.     public static void main(String[] args) {  
  6.          Scanner sca = new Scanner(System.in);  
  7.          int T = sca.nextInt(), t = 1;  
  8.          while(t <= T){  
  9.              BigInteger sum = BigInteger.ZERO;  
  10.              BigInteger zero =  BigInteger.ZERO;  
  11.              BigInteger tmp;  
  12.              while(sca.hasNextBigInteger()){  
  13.                  tmp = sca.nextBigInteger();  
  14.                  if(tmp.equals(zero))  
  15.                      break;  
  16.                  else  
  17.                      sum = sum.add(tmp);  
  18.              }  
  19.              System.out.println(sum);  
  20.              if(t != T)  
  21.                  System.out.println();  
  22.              t++;  
  23.          }  
  24.     }  
  25. }  

 

hdu1063

http://acm.hdu.edu.cn/showproblem.php?pid=1063

 

[java] view plain copy
 
  1. import java.math.BigDecimal;  
  2. import java.util.Scanner;  
  3.   
  4. public class Main {  
  5.     public static void main(String[] args) {  
  6.         Scanner sca = new Scanner(System.in);  
  7.          BigDecimal R,sum;  
  8.          int n;  
  9.          while(sca.hasNextBigDecimal()){  
  10.              sum = BigDecimal.ONE;  
  11.              R = sca.nextBigDecimal();  
  12.              n = sca.nextInt();  
  13.              for(int i = 1; i <= n; i++)  
  14.                  sum = sum.multiply(R);  
  15.              sum = sum.stripTrailingZeros();  
  16.              String str = sum.toPlainString();  
  17.              if(str.startsWith("0."))  
  18.                  str = str.substring(1);  
  19.              System.out.println(str);  
  20.          }  
  21.     }  
  22. }  

stripTrailingZeros
public BigDecimal stripTrailingZeros()
Returns a BigDecimal which is numerically equal to this one but with any trailing zeros removed from the representation. For example, stripping the trailing zeros from the BigDecimal value 600.0, which has [BigInteger, scale] components equals to [6000, 1], yields 6E2 with [BigInteger, scale] components equals to [6, -2]
Returns:a numerically equal BigDecimal with any trailing zeros removed.

 

toPlainString
public String toPlainString()
Returns a string representation of this BigDecimal without an exponent field. For values with a positive scale, the number of digits to the right of the decimal point is used to indicate scale. For values with a zero or negative scale, the resulting string is generated as if the value were converted to a numerically equal value with zero scale and as if all the trailing zeros of the zero scale value were present in the result. The entire string is prefixed by a minus sign character '-' ('\u002D') if the unscaled value is less than zero. No sign character is prefixed if the unscaled value is zero or positive. Note that if the result of this method is passed to the string constructor, only the numerical value of this BigDecimal will necessarily be recovered; the representation of the new BigDecimal may have a different scale. In particular, if this BigDecimal has a negative scale, the string resulting from this method will have a scale of zero when processed by the string constructor. (This method behaves analogously to the toString method in 1.4 and earlier releases.)
Returns:
a string representation of this BigDecimal without an exponent field.

startsWith
public boolean startsWith(String prefix)
Tests if this string starts with the specified prefix.
Parameters:prefix - the prefix.Returns:true if the character sequence represented by the argument is a prefix of the character sequence represented by this string; false otherwise. Note also that true will be returned if the argument is an empty string or is equal to this String object as determined by the equals(Object) method.

 

hdu1316

 

http://acm.hdu.edu.cn/showproblem.php?pid=1316

 

[java] view plain copy
 
  1. import java.math.BigInteger;  
  2. import java.util.Scanner;  
  3.   
  4. public class Main {  
  5.     public static void main(String[] args) {  
  6.         Scanner sca = new Scanner(System.in);  
  7.          int i,sum;  
  8.          BigInteger a,b;  
  9.          BigInteger f[] = new BigInteger[505];  
  10.          BigInteger zero = BigInteger.ZERO;  
  11.          f[1] = new BigInteger("1");  
  12.          f[2] = new BigInteger("2");  
  13.          for(i = 3; i <= 500; i++)  
  14.              f[i] = f[i - 1].add(f[i - 2]);  
  15.          while(sca.hasNextBigInteger()){  
  16.              sum = 0;  
  17.              a = sca.nextBigInteger();  
  18.              b = sca.nextBigInteger();  
  19.              if(a.equals(zero) && b.equals(zero))  
  20.                  break;  
  21.              for(i = 1; i <= 500; i++){  
  22.                  if(a.compareTo(f[i]) <= 0 && b.compareTo(f[i]) >= 0)  
  23.                      sum++;  
  24.                  if(b.compareTo(f[i]) < 0)  
  25.                      break;  
  26.              }  
  27.              System.out.println(sum);  
  28.          }  
  29.     }  
  30. }  


hdu1715

 

http://acm.hdu.edu.cn/showproblem.php?pid=1715

 

[java] view plain copy
 
  1. import java.math.BigInteger;  
  2. import java.util.Scanner;  
  3.   
  4. public class Main {  
  5.     public static void main(String[] args) {  
  6.         Scanner sca = new Scanner(System.in);  
  7.          int t = 1, T = sca.nextInt();  
  8.          BigInteger f[] = new BigInteger[1005];  
  9.          f[1] = f[2] = BigInteger.ONE;  
  10.          for(int i = 3; i <= 1000; i++)  
  11.              f[i] = f[i - 1].add(f[i - 2]);  
  12.          while(sca.hasNext()){  
  13.              int n = sca.nextInt();  
  14.              System.out.println(f[n]);  
  15.          }  
  16.     }  
  17. }  


hdu1753

 

http://acm.hdu.edu.cn/showproblem.php?pid=1753

 

[java] view plain copy
 
    1. import java.math.BigDecimal;  
    2. import java.util.Scanner;  
    3.   
    4. public class Main {  
    5.     public static void main(String[] args) {  
    6.         Scanner sca = new Scanner(System.in);  
    7.          BigDecimal a,b,c;  
    8.          while(sca.hasNext()){  
    9.              a = sca.nextBigDecimal();  
    10.              b = sca.nextBigDecimal();  
    11.              c = a.add(b);  
    12.              c = c.stripTrailingZeros();  
    13.              String str = c.toPlainString();  
    14.              if(str.startsWith("0."))  
    15.                  str = str.substring(1);  
    16.              System.out.println(str);  
    17.          }  
    18.     }  
    19. }  
posted @ 2018-04-23 07:29  岚之川  阅读(188)  评论(0编辑  收藏  举报