大数取余 java_java 大数计算

这几天做了几道用大数的题,发现java来做大数运算十分方便。对acmer来说是十分实用的

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.punlic int comareTo();

14.boolean equals(); 是否相等

15.BigInteger构造函数:

1 import java.io.*;2 importjava.math.BigInteger;3 import java.util.*;4 public classMain {5

6 public static voidmain(String[] args) {7 //TODO Auto-generated method stub

8 Scanner cin=new Scanner (newBufferedInputStream(System.in));9 PrintWriter cout=newPrintWriter(System.out);10 intt;11 t=cin.nextInt();12 int eg=1;13 while(t>0)14 {15 BigInteger eight=new BigInteger("8");16 BigInteger seven=new BigInteger("7");17 BigInteger one=new BigInteger("1");18 String inp;19 inp=cin.next();20 BigInteger n=newBigInteger(inp);21 BigInteger ans;22 BigInteger ans2;23 ans=n.multiply(n).multiply(eight);24 ans2=n.multiply(seven);25 ans=ans.subtract(ans2);26 ans=ans.add(one);27 cout.println("Case #"+eg+": "+ans);28 ++eg;29 --t;30

31 }32 cout.flush();33 }34

35 }

大数的所有计算都是与大数之间进行的

BIgInteger a,b;

a=a.add(b);

a=a.substract(b);

a=a.multiply(b);

a=a.divied(b);

a=a.gcd(b);

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值