大数的除法 不使用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 ...
随机推荐
- 个人作业-Week3
个人作业-Week3 1. 软件工程师的成长 同学们在上这门课的时候,还是大三,你的困难和迷茫,别人一定有过.请看看别人怎么学习的,有些是科班,有些是野路子,有些成功,有些失败. 请读完下面所有博客( ...
- easyui datagride 两种查询方式
easyui datagride 两种查询方式function doReseach() { //$('#tt').datagrid('load', { // FixedCompany: $('.c_s ...
- 分享自己的超轻量级高性能ORM数据访问框架Deft
Deft 简介 Deft是一个超轻量级高性能O/R mapping数据访问框架,简单易用,几分钟即可上手. Deft包含如下但不限于此的特点: 1.按照Transact-SQL的语法语义风格来设计,只 ...
- 建立docker私有库(docker registry)(转)
建立docker私有库(docker registry) 博客分类: docker 我的目标还是无互联网安装,部署内部的docker私有库,目前docker镜像的获得还是需要互联网,将下载好的do ...
- 关于freemaker的一点使用技巧
在做国泰君安2016年中秋送电影票活动中,需要做这样一个手机页面,展示所有中奖用户的中奖信息.如下图: 要求对用户的后记号码中间5位做隐藏处理.最开始的处理方法是在对用户实体的get()方法做处理 / ...
- sizeof、strlen、字符串、数组,整到一块,你还清楚吗?
写在前面 sizeof.strlen.字符串.数组,提到这些概念,相信学过C语言的人都能耳熟能详,也能谈得头头是道,但是,在实际运用中,当这些内容交织在一起时,大家却不一定能搞地清清楚楚,本文的目的正 ...
- slide效果
html和js部分 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://w ...
- oracle 自增ID
drop sequence SEQ_sys_dictionary; create sequence SEQ_sys_dictionary INCREMENT BY START WITH ; creat ...
- DTP激活时报Overlapping
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- swift开发学习网站
1.https://github.com/Aufree/trip-to-iOS#ios- 2.http://www.code4app.com/forum.php?mod=viewthread& ...