java 大数运算 BinInteger
来源:互联网 发布:北京110网络报警平台 编辑:程序博客网 时间:2024/05/17 12:03
在用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.punlic int comareTo();
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.*包中
大数阶乘
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=28
代码如下:
[java] view plaincopy
棋盘覆盖
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=45
代码如下:
[java] view plaincopy
比较大小
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=73
代码如下:
[java] view plaincopy
大数加法
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=103
代码如下:
[java] view plaincopy
递推求值
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=114
代码如下:
[java] view plaincopy
高精度幂
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=155
代码如下:
[java] view plaincopy
这两个类都在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.punlic int comareTo();
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.*包中
Scanner cin=new Scanner(System.in);// 读入while(cin.hasNext()) //等同于!=EOF{ int n; BigInteger m; n=cin.nextInt(); //读入一个int; m=cin.BigInteger();//读入一个BigInteger;System.out.print(m.toString());}
大数阶乘
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=28
代码如下:
[java] view plaincopy
import java.io.*; import java.math.BigInteger; import java.util.*; public class Main { public static void main(String args[]) { Scanner cin = new Scanner(System.in); int n = cin.nextInt(); BigInteger ans = BigInteger.ONE; for(int i = 1; i <= n; ++i) ans = ans.multiply(BigInteger.valueOf(i)); System.out.println(ans); } }
棋盘覆盖
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=45
代码如下:
[java] view plaincopy
import java.math.BigInteger; import java.util.*; import java.io.*; public class Main { public static void main(String args[]) { Scanner in = new Scanner(System.in); int test = in.nextInt(); while(test-- > 0) { int n; n = in.nextInt(); BigInteger a = new BigInteger("4"); for(int i = 1; i < n; ++i) a = a.multiply(BigInteger.valueOf(4)); System.out.println(a.subtract(BigInteger.valueOf(1)).divide(BigInteger.valueOf(3))); } } }
比较大小
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=73
代码如下:
[java] view plaincopy
import java.io.*; import java.math.BigInteger; import java.util.*; public class Main { public static void main(String args[]) { Scanner cin = new Scanner(System.in); while(cin.hasNext()) { BigInteger a = cin.nextBigInteger(); BigInteger b = cin.nextBigInteger(); if(a.equals(BigInteger.ZERO) && b.equals(BigInteger.ZERO)) break; int flag = a.compareTo(b); if(flag == -1) System.out.println("a<b"); else if(flag == 0) System.out.println("a==b"); else System.out.println("a>b"); } } }
大数加法
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=103
代码如下:
[java] view plaincopy
import java.math.BigInteger; import java.util.*; import java.io.*; public class Main { public static void main(String args[]) { Scanner in = new Scanner(System.in); int n = in.nextInt(); for(int i = 1; i <= n; ++i) { BigInteger a = in.nextBigInteger(); BigInteger b = in.nextBigInteger(); BigInteger ans = a.add(b); System.out.println("Case " + i + ":"); System.out.println(a + " + " + b + " = " +ans); } } }
递推求值
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=114
代码如下:
[java] view plaincopy
import java.io.*; import java.math.BigInteger; import java.util.*; public class Main { public static void main(String args[]) { Scanner cin = new Scanner(System.in); BigInteger a[] = new BigInteger[100]; while(cin.hasNext()) { for(int i = 0; i <= 2; ++i) a[i] = cin.nextBigInteger(); for(int i = 3; i <= 99; ++i) a[i] = a[i - 1].add(a[i - 2]).add(a[i - 3]); System.out.println(a[99]); } } }
高精度幂
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=155
代码如下:
[java] view plaincopy
import java.io.*; import java.math.BigDecimal; import java.util.*; public class Main { public static void main(String args[]) { Scanner cin = new Scanner(System.in); while(cin.hasNext()) { BigDecimal ans = cin.nextBigDecimal(); int n = cin.nextInt(); String res = ans.pow(n).stripTrailingZeros().toPlainString(); //整数去掉小数点和后面的0 if(res.startsWith("0")) //去掉前导0 { res = res.substring(1); } System.out.println(res); } } }
0 0
- java 大数运算 BinInteger
- java.math.BigDecimal、BinInteger类的用法(用于精确运算)
- Java中的大数运算
- JAVA大数运算
- java 大数运算
- java 大数运算[转]
- poj2305 JAVA大数运算
- JAVA 大数运算
- Java大数运算
- Java实现大数运算
- 【JAVA 大数运算】
- java大数运算运用
- java.math.BinInteger和java.math.BigDecimal
- java.math.BigDecimal、BinInteger类的用法
- java高精度、大数运算总结
- Java实现大数加法运算
- 大数运算(c、java)
- HDU1042&1753--大数运算--JAVA
- course3 二分
- UVA-442数据结构调用栈,注意括号的处理
- android canvas drawText \r\n 换行
- 学生信息管理系统 输入和输出 链表
- 设置窗口客户区大小的方法
- java 大数运算 BinInteger
- android 禁止横竖屏切换时activity重新启动
- 对ExecuteNonQuery,ExecuteScalar,DataSet 进行封装(params可变参数的用法)Sqlparameter
- ubuntu 下串口调试安装(gtkterm)
- 【c++系列】typeid操作符(判断类型是否相同)
- Back to Underworld
- Tplink 上网设置
- hdu4288 线段树+离线化+离散化
- 解决R.java无法自动生成id的问题