自己动手写Java大整数《3》除法和十进制转换
之前已经完毕了大整数的表示、绝对值的比較大小、取负值、加减法运算以及乘法运算。
详细见前两篇博客(自己动手写Java * )。
这里加入除法运算。
另外看到作者Pauls Gedanken在blog(http://paul-ebermann.tumblr.com/post/6312290327/big-numbers-selfmade-part-2-14-conversion-from)中的转换十进制数到大整数的方法,这里一并列出。
除法
除法使用经典的除法法则,可是有几个须要注意的问题,以下列出。
1,除数是0的问题;
2。两者相等,输出1余数是被除数;
3。除数比較大,直接输出0。余数是被除数
4,子数组长度和除数数组长度相等或者大1。相等时候结果由子数组第一位除以除数第一位决定,
大1时候结果通过子数组头两位除以除数第一位决定
5,判定结果第一位是否为0.
我们看下简单的图演示样例子。看看除法的详细操作
/*
* 除法
*/ public DecimalBig Divide(DecimalBig that)
{
if (that.Compare(Zero)==0)
throw new IllegalArgumentException("Divided by 0!");
if (this.Abscompare(that)==-1)
return Zero;
if (this.Abscompare(that)==0)
return One; int thislength=this.digits.length;
int thatlength=that.digits.length; int[] mulres=new int[thatlength];
System.arraycopy(this.digits, 0, mulres, 0, thatlength);
int digitdivide=0;
int[] result=new int[thislength-thatlength+1]; for (int i=0; i<thislength-thatlength;i++)
{
if (Arraycompare(mulres,that.digits)==-1)
{
digitdivide=0;
} else
{
if (mulres.length==that.digits.length)
digitdivide=mulres[0]/that.digits[0];
else{
long firtwei=(long)mulres[0]*Radix+mulres[1];
digitdivide=(int)firtwei/that.digits[0]; } int[] temp=Multiplyint(that.digits, digitdivide); if (Arraycompare(mulres,temp)==-1){
digitdivide-=1;
temp=Substract(temp,that.digits);
}
mulres=Substract(mulres, temp);
} result[i]=digitdivide;
if (mulres.length==1&&mulres[0]==0){
mulres[0]=this.digits[i+thatlength];
}else{
int[] temp2=new int[mulres.length+1];
System.arraycopy(mulres, 0, temp2, 0, mulres.length);
temp2[mulres.length]=this.digits[i+thatlength];
mulres=temp2;
}
} if (Arraycompare(mulres,that.digits)==-1)
{
digitdivide=0;
} else
{
if (mulres.length==that.digits.length)
digitdivide=mulres[0]/that.digits[0];
else{
long firtwei=(long)mulres[0]*Radix+mulres[1];
long ttt=firtwei/that.digits[0];
digitdivide=(int) ttt;
} int[] temp=Multiplyint(that.digits, digitdivide); if (Arraycompare(mulres,temp)==-1){
digitdivide-=1;
temp=Substract(temp,that.digits);
}
mulres=Substract(mulres, temp);
} /*
* 最后的mulres就是余数。
*/ result[thislength-thatlength]=digitdivide;
/*
* 去掉首位的零
*/
int i=0;
while(i<result.length&&result[i]==0)
i++;
//截取非零项
int[] temp = new int[result.length-i];
System.arraycopy(result, i, temp, 0, result.length-i);
result = temp; return new DecimalBig(1, result);
} /*
* 除法余数
*/ public DecimalBig DivideReminder(DecimalBig that)
{
if (that.Compare(Zero)==0)
throw new IllegalArgumentException("Divided by 0!");
if (this.Abscompare(that)==-1)
return this;
if (this.Abscompare(that)==0)
return Zero; int thislength=this.digits.length;
int thatlength=that.digits.length; int[] mulres=new int[thatlength];
System.arraycopy(this.digits, 0, mulres, 0, thatlength);
int digitdivide=0;
int[] result=new int[thislength-thatlength+1]; for (int i=0; i<thislength-thatlength;i++)
{
if (Arraycompare(mulres,that.digits)==-1)
{
digitdivide=0;
} else
{
if (mulres.length==that.digits.length)
digitdivide=mulres[0]/that.digits[0];
else{
long firtwei=(long)mulres[0]*Radix+mulres[1];
digitdivide=(int)firtwei/that.digits[0]; } int[] temp=Multiplyint(that.digits, digitdivide); if (Arraycompare(mulres,temp)==-1){
digitdivide-=1;
temp=Substract(temp,that.digits);
}
mulres=Substract(mulres, temp);
} result[i]=digitdivide;
if (mulres.length==1&&mulres[0]==0){
mulres[0]=this.digits[i+thatlength];
}else{
int[] temp2=new int[mulres.length+1];
System.arraycopy(mulres, 0, temp2, 0, mulres.length);
temp2[mulres.length]=this.digits[i+thatlength];
mulres=temp2;
}
} if (Arraycompare(mulres,that.digits)==-1)
{
digitdivide=0;
} else
{
if (mulres.length==that.digits.length)
digitdivide=mulres[0]/that.digits[0];
else{
long firtwei=(long)mulres[0]*Radix+mulres[1];
long ttt=firtwei/that.digits[0];
digitdivide=(int) ttt;
} int[] temp=Multiplyint(that.digits, digitdivide); if (Arraycompare(mulres,temp)==-1){
digitdivide-=1;
temp=Substract(temp,that.digits);
}
mulres=Substract(mulres, temp);
} /*
* 最后的mulres就是余数。 */ return new DecimalBig(1, mulres);
}
十进制转换
引自Pauls Gedanken的方法
/**引用
* Pauls Gedanken在bloghttp://paul-ebermann.tumblr.com/post/6312290327/big-numbers-selfmade-part-2-14-conversion-from
* 中的方法
* creates a DecimalBigInt from a decimal representation.
* @param decimal a string of decimal digits.
* @throws NumberFormatException if the number is not in
* correct decimal format, e.g. if it contains any characters
* outside of 0..9.
*/
public static DecimalBig valueOf(int si, String decimal) {
int decLen = decimal.length();
int bigLen = (decLen-1) / Radix_Decimal_length + 1;
// length of first block
int firstSome = decLen - (bigLen-1) * Radix_Decimal_length;
int[] digits = new int[bigLen];
for(int i = 0; i < bigLen ; i++) {
String block =
decimal.substring(Math.max(firstSome + (i-1)*Radix_Decimal_length, 0),
firstSome + i *Radix_Decimal_length);
digits[i] = Integer.parseInt(block);
}
return new DecimalBig(si, digits);
}
自己动手写Java大整数《3》除法和十进制转换的更多相关文章
- [JVM] - 一份<自己动手写Java虚拟机>的测试版
go语言下载 配置GOROOT(一般是自动的),配置GOPATH(如果想自己改的话) 参照<自己动手写Java虚拟机> > 第一章 指令集和解释器 生成了ch01.exe文件 这里还 ...
- 自己动手写java 字节流输入输出流
数据流是一串连续不断的数据的集合,就象水管里的水流,在水管的一端一点一点地供水,而在水管的另一端看到的是一股连续不断的水流. "流是磁盘或其它外围设备中存储的数据的源点或终点." ...
- 关于Java大整数是否是素数
题目描述 请编写程序,从键盘输入两个整数m,n,找出等于或大于m的前n个素数. 输入格式: 第一个整数为m,第二个整数为n:中间使用空格隔开.例如: 103 3 输出格式: 从小到大输出找到的等于或大 ...
- 自己动手写java锁
1.LockSupport的park和unpark方法的基本使用,以及对线程中断的响应性 LockSupport是JDK中比较底层的类,用来创建锁和其他同步工具类的基本线程阻塞原语.java锁和同步器 ...
- JAVA大整数傻瓜入门
http://blog.csdn.net/skiffloveblue/article/details/7032290..先记着
- java 中整数类型的进制转换
int a=10; Integer.toBinaryString(a); //转换成2进制Integer.toOctalString(a); //转换成8进制Integer.toHexString( ...
- [大整数乘法] java代码实现
上一篇写的“[大整数乘法]分治算法的时间复杂度研究”,这一篇是基于上一篇思想的代码实现,以下是该文章的连接: http://www.cnblogs.com/McQueen1987/p/3348426. ...
- Coefficient Computation (大整数、Java解决)
Coefficient Computation UVALive8265 题意:计算组合数C(n,k)的值并将值按给定的进制输出. 思路:Java大整数类硬上. PS:刚刚学完Java的大整数类,结果却 ...
- COJ 1211 大整数开平方
手写求大整数开根号所得到的值,具体计算过程参考别人的资料,最后利用java的大整数得到答案 别人博客链接:http://www.cnblogs.com/Rinyo/archive/2012/12/16 ...
随机推荐
- USACO Section 5.1 Musical Themes(枚举)
直接枚举O(n^3)会TLE,只要稍微加点优化,在不可能得到更优解时及时退出.其实就是道水题,虽说我提交了6次才过= =..我还太弱了 -------------------------------- ...
- python中,str和repr的区别
str函数,它会把值转换为合理形式的字符串,以便用户可以理解. repr会创建一个字符串,它以合法的Python表达式的形式来表示值. 例如: >>> print repr(&quo ...
- javascript笔记—面向对象
什么是对象: 对象是一个整体,对外提供一些操作. 什么是面向对象: 使用对象时,只关注对象提供的功能,不关注其内部细节,例如jquery 面向对象是一种通用思想,并非只有编程中能用,任何事情都可以用. ...
- 一周学会Mootools 1.4中文教程:(4)类型
Mootools的类型主要包含下边几部分:String:字符串;Number:数字;Array:数组;Object:对象;Json:;Cookie:. 这也是我们今天的讲述重点.每一种数据类型Mt都为 ...
- codevs 1455 路径 计算m^n%p
题目链接 题目描述 Description 小明从A1到An+1,他知道从A1到A2,从A2到A3,......,从An到An+1都有m条路,且从A1到An+1都只有这些路.小明想知道,从A1地到An ...
- Archlinux在Btrfs分区上的安装(bios篇)
其实本文所有的内容在Archwiki上都可以找到,并且更新更全面(只是比较零散),我所做的只是对安装流程做一个小小的总结,每一步我都会稍微解释一下,但不会说的特别详细,毕竟这只是一篇安装引导文,而不是 ...
- MYSQL group_concat() 函数
看来看一下表中的数据 select * from t; 下一步来看一下group_concat函数的用法 select ID,group_concat(Name) from t group by ID ...
- docker基础入门之一
一.概述 1.传统虚拟化技术: 纯软件的虚拟化是通过对于硬件层的模拟从而实现允许运行多个操作系统: 硬件辅助虚拟化需要硬件层面对于虚拟化的支持,类似Intel-VT技术等,具有更高的运行效率: 解决方 ...
- iOS 百度地图大头针使用
百度地图使用第五讲:大头针使用(地图标注)http://bbs.yusian.com/thread-8384-1-1.html(出处: 小龙虾IT笔记)
- iOS 中多线程的简单使用
iOS中常用的多线程操作有( NSThread, NSOperation GCD ) 为了能更直观的展现多线程操作在SB中做如下的界面布局: 当点击下载的时候从网络上下载图片: - (void)loa ...