ACM之Java速成(3)

ACM中Java.大数处理

 

先上个代码:

 1 import java.math.*;
 2 import java.util.*;
 3 class  Main{
 4 public static void main(String args[]){
 5     Scanner cin=new Scanner(System.in);
 6     BigInteger a,b,t;
 7     b=BigInteger.valueOf(17);
 8     t=BigInteger.valueOf(0);
 9         while(cin.hasNext())
10         {
11             a=cin.nextBigInteger();
12             if(a.compareTo(t)==0)break;
13             if(a.mod(b).compareTo(t)==0)
14             {
15                 System.out.println("1");
16             }
17             else System.out.println("0");
18         }
19     }
20 }

 

BigInteger和BigDecimal可以说是acmer选择java的首要原因。
函数:add, subtract, divide, mod, compareTo等,其中加减乘除模都要求是BigInteger(BigDecimal)和BigInteger(BigDecimal)之间的运算,所以需要把int(double)类型转换为BigInteger(BigDecimal),用函数BigInteger.valueOf().


BigInteger
主要API
将字符串转换成BigInteger
BigInteger(String val)
将 BigInteger 的十进制字符串表示形式转换为 BigInteger。
BigInteger(String val, int radix)
将指定基数的 BigInteger 的字符串表示形式转换为 BigInteger。

BigInteger的加法
BigInteger
add(BigInteger val)
返回其值为 (this + val) 的 BigInteger。
BigInteger
and(BigInteger val)
返回其值为 (this & val) 的 BigInteger。


BigInteger的减法
BigInteger
subtract(BigInteger val)
返回其值为 (this - val) 的 BigInteger。


BigInteger的乘法
BigInteger
multiply(BigInteger val)
返回其值为 (this * val) 的 BigInteger。


大数求余:
BigInteger
mod(BigInteger m)
返回其值为 (this mod m) 的 BigInteger。


大数除法
BigInteger
divide(BigInteger val)
返回其值为 (this / val) 的 BigInteger。

其他一些
BigInteger
gcd(BigInteger val)
返回一个 BigInteger,其值是 abs(this) 和 abs(val) 的最大公约数。


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



BigDecimal类
主要API:
将字符串转换成BigDecimal
BigDecimal(String val)
将 BigDecimal 的字符串表示形式转换为 BigDecimal。
BigDecimal(String val, MathContext mc)
将 BigDecimal 的字符串表示形式转换为 BigDecimal,接受与 BigDecimal(String) 构造方法相同的字符串(按照上下文设置进行舍入)。


两个BigDecimal的相加
BigDecimal
add(BigDecimal augend)
返回一个 BigDecimal,其值为 (this + augend),其标度为 max(this.scale(), augend.scale())。
BigDecimal
add(BigDecimal augend, MathContext mc)
返回其值为 (this + augend) 的 BigDecimal(根据上下文设置进行舍入)。


两个BigDecimal的相减
BigDecimal
subtract(BigDecimal subtrahend)
返回一个 BigDecimal,其值为 (this - subtrahend),其标度为 max(this.scale(), subtrahend.scale())。
BigDecimal
subtract(BigDecimal subtrahend, MathContext mc)
返回其值为 (this - subtrahend) 的 BigDecimal(根据上下文设置进行舍入)。


两个BigDecimal的相除:
BigDecimal
divide(BigDecimal divisor)
返回一个 BigDecimal,其值为 (this / divisor),其首选标度为 (this.scale() - divisor.scale());如果无法表示准确的商值(因为它有无穷的十进制扩展),则抛出 ArithmeticException。
BigDecimal
divide(BigDecimal divisor, int roundingMode)
返回一个 BigDecimal,其值为 (this / divisor),其标度为 this.scale()。
BigDecimal
divide(BigDecimal divisor, int scale, int roundingMode)
返回一个 BigDecimal,其值为 (this / divisor),其标度为指定标度。
BigDecimal
divide(BigDecimal divisor, int scale, RoundingMode roundingMode)
返回一个 BigDecimal,其值为 (this / divisor),其标度为指定标度。
BigDecimal
divide(BigDecimal divisor, MathContext mc)
返回其值为 (this / divisor) 的 BigDecimal(根据上下文设置进行舍入)。
BigDecimal
divide(BigDecimal divisor, RoundingMode roundingMode)
返回一个 BigDecimal,其值为 (this / divisor),其标度为 this.scale()。

计算BigDecimal的N次幂
BigDecimal
pow(int n)
返回其值为 (thisn) 的 BigDecimal,准确计算该幂,使其具有无限精度。
BigDecimal
pow(int n, MathContext mc)
返回其值为 (thisn) 的 BigDecimal。



有关转换成字符串的方法
String
toEngineeringString()
返回此 BigDecimal 的字符串表示形式,需要指数时,则使用工程计数法。
String
toPlainString()
返回不带指数字段的此 BigDecimal 的字符串表示形式。
String
toString()
返回此 BigDecimal 的字符串表示形式,如果需要指数,则使用科学记数法。

 

 大数相除:

 1 import java.math.*;
 2 import java.util.*;
 3 class  Main{
 4 public static void main(String args[]){
 5     Scanner cin=new Scanner(System.in);
 6     BigInteger a,b,t;
 7     b=BigInteger.valueOf(17);
 8     t=BigInteger.valueOf(0);
 9         while(cin.hasNext())
10         {
11             a=cin.nextBigInteger();
12             if(a.compareTo(t)==0)break;
13             if(a.mod(b).compareTo(t)==0)
14             {
15                 System.out.println("1");
16             }
17             else System.out.println("0");
18         }
19     }
20 }

 

 

A+B problem IV

 1 import java.math.*;
 2 import java.util.*;
 3 class  Main{
 4 public static void main(String args[]){
 5     Scanner cin=new Scanner(System.in);
 6     BigDecimal a,b,s,t;
 7     t=BigDecimal.valueOf(0);
 8         while(cin.hasNext())
 9         {
10         a=cin.nextBigDecimal();
11         b=cin.nextBigDecimal();
12         
13         if(a.compareTo(t)==0&&b.compareTo(t)==0)
14             System.out.println("0");
15         else
16         {
17             s=a.add(b);
18             System.out.println(s.stripTrailingZeros().toPlainString());
19         }
20         } 
21     }
22 }        

 

 

高精度求幂

 1 //高精度求幂
 2 import java.util.*;
 3 import java.io.*;
 4 import java.math.*;
 5 
 6 public class Main {
 7      public static void main(String[] arg){
 8           Scanner cin=new Scanner(new BufferedInputStream(System.in));
 9           BigDecimal a;
10           for(;cin.hasNext();)
11           {
12            a=cin.nextBigDecimal();
13            a=a.pow(cin.nextInt()).stripTrailingZeros();
14            System.out.println(a.toPlainString().replaceAll("^0", ""));
15           }
16      }
17 }

 

 

 

 

未完待续,,,

 

 

 

 

转载于:https://www.cnblogs.com/jeff-wgc/p/4480047.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值