大数的除法 不使用BigInteger Java实现
import java.util.Arrays;
public class Solution {
public void div(String a, String b) {
char[] chara = a.toCharArray();
char[] charb = b.toCharArray();
int[] numa = new int[a.length()];
int[] numb = new int[b.length()];
for (int i = 0; i < charb.length; i++) {
numb[i] = charb[i]-'0';
// System.out.print(""+numb[i]);
}
System.out.println("");
for (int i = 0; i < chara.length; i++) {
numa[i] = chara[i]-'0';
System.out.print(""+numa[i]);
}
System.out.println("");
int[] div = new int [1000];
int count = 0;
while(compare(numa,numb)>0){
numa= div(numa,numb);
addSelf(div,count);
}
StringBuffer sb = new StringBuffer();
sb.append(""+div+" ");
for (int i = 0; i < numa.length; i++) {
sb.append(""+numa[i]);
}
System.out.println(""+sb);
}
private void addSelf(int[] div, int count) {
if (div[div.length-1]!=9) {
div[div.length-1]++;
}else{
int i = div.length-1;
while (div[i]==9){
div[i] = 0;
i--;
} ;
if (div[i]==0) {
count++;
}else{
div[i] ++;
}
}
}
private int[] div(int[] numa, int[] numb) {
for (int i = 0; i < numb.length; i++) {
int tmpa = numa[numa.length-i-1];
int tmpb = numb[numb.length-i-1];
int sub = tmpa-tmpb;
int count = numa.length-1;
if (sub>=0) {
numa[numa.length-i-1] = sub;
}else{
numa[numa.length-i-1] = sub+10;
count =numa.length- i-1-1;
while(numa[count]==0){
numa[count] =9;
count--;
}
numa[count]--;
if (numa[0]==0) {
numa = Arrays.copyOfRange(numa,1, numa.length);
}
}
}
for (int i = 0; i < numa.length; i++) {
// System.out.print(""+numa[i]);
}
//System.out.println("");
return numa;
}
int compare(int[] numa, int[] numb) {
if (numa.length<numb.length) {
return -1;
}else if (numa.length>numb.length) {
return 1;
}else{
for (int i = 0; i < numb.length; i++) {
if (numa[i]>numb[i]) {
return 1;
}
if (numa[i]<numb[i]) {
return -1;
}
}
return 0;
}
}
public static void main(String[] args) {
Solution solution = new Solution();
String a = "1234578901234567890123456789";
String b = "1";
solution.div(a, b);
}
}
大数的除法 不使用BigInteger Java实现的更多相关文章
- 【Java】-BigInteger大数类的使用【超强Java大数模板 总结】
Scanner cin = new Scanner(new BufferedInputStream(System.in)); 这样定义Scanner类的对象读入数据可能会快一些! 参考这个博客继续补充 ...
- 1030 大数进制转换(51Nod + JAVA)
题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1030 题目: 代码实现如下: import java.mat ...
- HDU 1134 卡特兰数 大数乘法除法
Problem Description This is a small but ancient game. You are supposed to write down the numbers 1, ...
- java精确除法计算,四舍五入 Java问题通用解决代码
主要用java.math.BigDecimal工具类实现,想要了解BigDecimal类可以看java api 正式版: public static Double divide() ...
- Java大数处理类:BigInteger类和BigDecimal类
当我们要处理非常大的数据时,平常用的数据类型已不足以表示,在Java中有两个类BigInteger和BigDecimal分别表示大整数类和大浮点数类,这两个类在理论上只要计算机内存足够大就能够表示无线 ...
- 大数运算之 Java BigInteger 的基本用法
大数运算之 Java BigInteger 的基本用法 在程序设计竞赛中会遇到高精度运算的问题,C++没有高精度运算,只能手动模拟人工运算,手动实现高精度,而 java.math 包中的 BigInt ...
- Java实现大数相加、相乘(不使用BigInteger)
大数相加: package algorithm; //使用BigInteger类验证 import java.math.BigInteger; public class BigAdd { public ...
- JAVA大数处理(BigInteger,BigDecimal)
原文链接 Java中有两个类BigInteger和BigDecimal分别表示大整数类和大浮点数类. 这两个类都在java.math.*包中,因此每次必须在开头处引用该包. Ⅰ基本函数: 1.valu ...
- java大数相加
import java.math.BigInteger; import java.util.Scanner; public class Bignum{ public static void ma ...
随机推荐
- Boost学习笔记(三) progress_timer
progress_timer也是一个计时器,它继承自timer,会在析构时自动输出时间,省去timer手动调用elapsed()的工作,是一个用于自动计时相当方便的小工具. #include < ...
- Arduino入门笔记【1】
刚刚接触这个东西只知道这是类似于单片机的开发板,可以做一些单片机实现或者不能实现的东西,但是比单片机要简单得多. Arduino到底是什么? 维基百科上的描述是:Arduino是一块开发板的微控制器和 ...
- T-SQL编程练习(带注释)
use test; GO /*创建自定义函数的格式: * create function 函数名(参数 数据类型) * returns 返回数据类型 as * begin * 代码 * end */ ...
- pandas处理数据1
读文件 pd.read_csv('path/to/file.txt',header=0,names='ab',index_col=0) names Columns这个可以不写,制定索引列是第一列,这样 ...
- html5画布基础
canvas 元素用于在网页上绘制图形. 什么是canvas? HTML5 的 canvas 元素使用 JavaScript 在网页上绘制图像. 画布是一个矩形区域,您可以控制其每一像素. canva ...
- linux下使用fork,exec,waitpid模拟system函数
代码如下: #include <sys/types.h> #include <sys/wait.h> #include <unistd.h> #include &l ...
- Asp.Net_Mvc_@Html.xxx()的扩展
/// <summary> /// 生成分类下拉-列表框,选中指定的项 /// </summary> /// <param name="html"&g ...
- HTML5 十大新特性(十)——Web Socket
webSocket是H5新加的一个协议,为了解决http协议的request.response一一对应和它自身的被动性,以及ajax轮询等问题.一方可以发送多条信息,连接不中断,永久连接,但也导致了服 ...
- Oracle10G无图形安装及升级
Oracle10.2.0.1静默安装及升级到10.2.0.4 下载及解压好database和Disk1 环境配置: su - oracle vim ~/.bash_profile 保存. vim /d ...
- Excel数据批量导入到数据库
1.今天做批量导入网上找了个例子,改了改,运行起来了.用POI实现Excel的读取,需要jar包. 2.ReadExcel.java读取数据 /** * */ package com.b510.exc ...