java大数处理——转载
来源:互联网 发布:mac bootcamp 首选系统 编辑:程序博客网 时间:2024/06/16 15:25
转载自http://hi.baidu.com/dolphin0520/blog/item/a0c88b1eb8ac86c4a786699e.html 在用C或者C++处理大数时感觉非常麻烦,但是在JAVA中有两个类BigInteger和BigDecimal分别表示大整数类和大浮点数类,至于两个类的对象能表示最大范围不清楚,理论上能够表示无线大的数,只要计算机内存足够大。 这两个类都在java.math.*包中,因此每次必须在开头处引用该包。 Ⅰ基本函数: 1.valueOf(parament); 将参数转换为制定的类型 比如 int a=3; BigIntegerb=BigInteger.valueOf(a); 则b=3; Strings=”12345”; BigIntegerc=BigInteger.valueOf(s); 则c=12345; 2.add(); 大整数相加 BigInteger a=newBigInteger(“23”); BigInteger b=newBigInteger(“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,intradix); 将指定基数的 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.Scanner; public class Main
import java.math.*;
import java.text.*;
{
public static void main(String args[])
{
Scanner cin = new Scanner ( System.in );
BigInteger a,b;
int c;
char op;
String s;
while( cin.hasNext() )
{
a = cin.nextBigInteger();
s = cin.next();
op = s.charAt(0);
if( op == '+')
{
b = cin.nextBigInteger();
System.out.println(a.add(b));
}
else if( op == '-')
{
b = cin.nextBigInteger();
System.out.println(a.subtract(b));
}
else if( op == '*')
{
b = cin.nextBigInteger();
System.out.println(a.multiply(b));
}
else
{
BigDecimal a1,b1,eps;
String s1,s2,temp;
s1 = a.toString();
a1 = new BigDecimal(s1);
b = cin.nextBigInteger();
s2 = b.toString();
b1 = new BigDecimal(s2);
c = cin.nextInt();
eps = a1.divide(b1,c,4);
//System.out.println(a + " " + b + "" + c);
//System.out.println(a1.doubleValue() + " "+ b1.doubleValue() + " " + c);
System.out.print( a.divide(b) + " " +a.mod(b) + " ");
if( c != 0)
{
temp = "0.";
for(int i = 0; i < c; i ++) temp +="0";
DecimalFormat gd = new DecimalFormat(temp);
System.out.println(gd.format(eps));
}
else System.out.println(eps);
}
}
}
}
- java大数处理——转载
- 黑马程序员——java中对大数的处理
- java 处理大数
- JAVA大数的处理
- java 处理大数
- JAVA 处理 大数 POJ1001
- Java中处理大数
- JAVA大数处理
- JAVA的大数处理
- Java大数处理
- Java大数处理
- JAVA之大数处理
- Java大数处理
- java处理大数类
- Java处理大数
- Java大数处理
- hduoj4762(Java处理大数)
- 用JAVA处理大数
- __declspec(dllexport) and __declspec(dllimport)
- SQL Server 2005技术内幕:查询、调整和优化1——扫描及查找
- SQL Server 2005技术内幕:查询、调整和优化2——Bookmark Lookup
- Jquery总结
- SQL Server 2005技术内幕:查询、调整和优化3——连接
- java大数处理——转载
- SQL Server 2005技术内幕:查询、调整和优化4——聚合
- jsp的内置对象
- SQL Server 2005技术内幕:查询、调整和优化5——用C#实现SQL查询
- web.xml中servlet, filter, listenr的加载顺序
- SQL Server 2005技术内幕:查询、调整和优化6——子查询
- 优秀程序员的45个习惯
- 金山开源之所想
- ophone刷机