JAVA大数处理(BigInteger,BigDecimal)
2016-05-18 23:22
417 查看
在用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.*包中
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());
}
import java.util.*;
import java.math.*;
public class Main
{
public static void main(String[] args)
{
Scanner cin = new Scanner ( System.in );
BigInteger a,b;
int c;
while( cin.hasNext() )
{
a=cin.nextBigInteger();
b=cin.nextBigInteger();
c=cin.nextInt();
System.out.println(a.add(b));
System.out.println(a.subtract(b));
System.out.println(a.multiply(b));
System.out.println(a.remainder(b));
System.out.println(a.pow(c));
System.out.println(a.gcd(b));
System.out.println(a.negate());
System.out.println(a.mod(b));
}
}
}
这两个类都在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());
}
import java.util.*;
import java.math.*;
public class Main
{
public static void main(String[] args)
{
Scanner cin = new Scanner ( System.in );
BigInteger a,b;
int c;
while( cin.hasNext() )
{
a=cin.nextBigInteger();
b=cin.nextBigInteger();
c=cin.nextInt();
System.out.println(a.add(b));
System.out.println(a.subtract(b));
System.out.println(a.multiply(b));
System.out.println(a.remainder(b));
System.out.println(a.pow(c));
System.out.println(a.gcd(b));
System.out.println(a.negate());
System.out.println(a.mod(b));
}
}
}
相关文章推荐
- Java垃圾回收机制--入门
- Java之字符串详解
- java多线程
- 算法(第四版)笔记<一>-------动态队列的数组实现(Java语言)
- Spring学习(二)Spring简介
- java.security.spec.InvalidKeySpecException DerInputStream.getLength(): lengthTag=111, too big.
- Springboot入门(使用指南)
- 线程同步和异步问题-Java
- Spring3.1+Hibernate3+Struts2的最新整合所需要的jar包
- Spring用代码来读取properties文件
- Struts2直接访问Servlet API(IoC--不推荐使用,非IoC---推荐使用)
- java基础回顾_day06_面向对象
- springmvc发布rest服务
- JAVA 类 URL
- Java环境搭建之MyEclipse优化技巧
- Eclipse导入库以后,properties-Android里面依然是红叉
- 安装 Hbase
- 过滤器和拦截器
- JAVA设计模式(12):结构型-门面模式(Facade)
- Solr 读写(Java SolrJ)