大数的除法 不使用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 ...
随机推荐
- SSO单点登录Spring-Security & CAS使用手册
1.1概述 1.1.1单点登录介绍 单点登录(Single Sign On , 简称 SSO )是目前比较流行的服务于企业业务整合的解决方案之一, SSO 使得在多个应用系统中,用户只需要登录一次就可 ...
- 2016ACM/ICPC亚洲区沈阳站-重现赛
C.Recursive sequence 求ans(x),ans(1)=a,ans(2)=b,ans(n)=ans(n-2)*2+ans(n-1)+n^4 如果直接就去解...很难,毕竟不是那种可以直 ...
- ProcessBuilder 、Runtime和Process 的区别
1.版本原因 ProcessBuilder是从java1.5加进来的,而exec系列方法是从1.0开始就有的,后续版本不断的重载这个方法,到了1.5已经有6个之多. 2.ProcessBuilder. ...
- Spring MVC控制器
Spring MVC中控制器用于解析用户请求并且转换为模型以提供访问应用程序的行为,通常用注解方式实现. org.springframework.stereotype.Controller注解用于声明 ...
- IOS8解决获取位置坐标信息出错(Error Domain=kCLErrorDomain Code=0)(转)
标题:IOS8解决获取位置坐标信息出错(Error Domain=kCLErrorDomain Code=0) 前几天解决了在ios8上无法使用地址位置服务的问题,最近在模拟器上调试发现获取位置坐标信 ...
- codeforces 744C Hongcow Buys a Deck of Cards
C. Hongcow Buys a Deck of Cards time limit per test 2 seconds memory limit per test 256 megabytes in ...
- es6还欠完善的地方
const的可变性 const用于声明常量. 什么是常量,声明后的值不可更改. 对于值类型,比如string,number等等.const声明确实有效. const str = "strin ...
- python走起之第十二话
1. ORM介绍 orm英文全称object relational mapping,就是对象映射关系程序,简单来说我们类似python这种面向对象的程序来说一切皆对象,但是我们使用的数据库却都是关系型 ...
- 理解RESTful
REST全称为Representational State Transfer,可以翻译为“表现状态转换”,是由是Roy Thomas Fielding在他2000年的博士论文中提出的,目的是为了得到一 ...
- android线程登录
主入口代码: package com.tp.soft.app; import java.io.IOException; import java.util.HashMap; import java.ut ...