大数加减法 - java实现

计算机处理的各种数据类型都有个范围,超出范围的就处理不了。

如果做超大数运算加减乘除,普通方法肯定是不行的,那么我们遇到大数的运算怎么处理呢?今天介绍一种大数加减乘除运算的方法

思路:

1. 将两个特大的整数利用字符数组作为存储介质。

2. 逐位计算 遍历结果逢十进一。

3. 对存储结果的数组进行翻转处理。

下面上代码:

 1 public class LargeIntSub {
 2     public static void main(String[] args) {
 3
 4         String a="6789";
 5         String b="123";
 6         int []pa=stringToInts(a);
 7         int []pb=stringToInts(b);
 8         String result_sub=sub(pa,pb);
 9         System.out.println("sub result is:"+result_sub);
10     }
11
12     public static int[] stringToInts(String s){
13         int[] n = new int[s.length()];
14         for(int i = 0;i<s.length();i++){
15         n[i] = Integer.parseInt(s.substring(i,i+1));
16         }
17         return n;
18     }
19
20     public static String sub(int []a,int []b){
21         StringBuffer sb=new StringBuffer();
22         boolean flag=false;
23         if(a.length<b.length){
24             int c[]=a;
25             a=b;b=c;
26             flag=true;
27         }
28         int a_len= a.length-1;
29         int b_len=b.length-1;
30         int degrade=0;
31         while(a_len>=0||b_len>=0){
32             int temp=0;
33             if(a_len>=0&&b_len>=0){
34                 if((a[a_len]-degrade)<b[b_len]){
35                     temp=a[a_len]+10-b[b_len]-degrade;
36                     degrade=1;
37                 }else{
38                     temp=a[a_len]-b[b_len]-degrade;
39                 }
40             }else if(a_len>=0){
41                 temp=a[a_len]-degrade;
42                 degrade=0;
43             }
44             sb.append(temp+"");
45
46             a_len--;b_len--;
47         }
48         if(flag){
49             return getNum(sb.append("-").reverse());
50         }
51         return getNum(sb.reverse());
52     }
53     public static String getNum(StringBuffer sb){
54         while(sb.length() > 1 && sb.charAt(0) == ‘0‘) {
55             sb.deleteCharAt(0);
56         }
57         return sb.toString();
58     }
59
60 }

原文地址:https://www.cnblogs.com/clarke157/p/8686009.html

时间: 2024-07-29 06:40:36

大数加减法 - java实现的相关文章

大数加减法总结

大数加减法总结(包括整数或者负数): 1.先解决不带符号的数的加减法 2.根据加数或者减数的符号位判断该选择加法还是减法计算,并且赋予结果对应的符号 需要注意的是:不带符号的减法产生的结果可能高位为'0',要进行处理. string bigNumberMinusWithoutSign(const char* num1, const char *num2){ string res = ""; bool flag1 = false; // 正负标志位 int flag = 0;//退位标志

大明A+B(hdu1753)大数,java

大明A+B Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 8483 Accepted Submission(s): 3000 Problem Description 话说,经过了漫长的一个多月,小明已经成长了许多,所以他改了一个名字叫“大明”.这时他已经不是那个只会做100以内加法的那个“小明”了,现在他甚至会任意长度的正小数的加法. 现在,

hdu 1002大数(Java)

A + B Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 230395    Accepted Submission(s): 44208 Problem Description I have a very simple problem for you. Given two integers A and B, you

大数乘法java版

1 import java.util.*; 2 import java.math.*; 3 4 public class NumMul{ 5 public static void main(String args[]){ 6 Scanner cin = new Scanner(System.in); 7 BigInteger a, b; 8 while(cin.hasNext()){ 9 a = cin.nextBigInteger(); 10 b = cin.nextBigInteger();

POJ 2756 Autumn is a Genius 大数加减法

Description Jiajia and Wind have a very cute daughter called Autumn. She is so clever that she can do integer additions when she was just 2 years old! Since a lot of people suspect that Autumn may make mistakes, please write a program to prove that A

Multiply Strings 大数相乘 java

先贴上代码 1 public String multiply(String num1, String num2) { 2 String str = ""; 3 StringBuffer sb = new StringBuffer(num1); 4 num1 = sb.reverse().toString(); 5 sb = new StringBuffer(num2); 6 num2 = sb.reverse().toString(); 7 int[] res = new int[nu

大数加法java版

1 import java.util.*; 2 import java.math.BigDecimal; 3 4 public class Numadd{ 5 public static void main(String[] args){ 6 String s1,s2; 7 Scanner cin = new Scanner(System.in); 8 s1 = cin.next(); 9 s2 = cin.next(); 10 BigDecimal b1 = new BigDecimal(s1

区间K大数查找 Java 蓝桥杯ALGO-1

1 import java.util.Arrays; 2 import java.util.Scanner; 3 4 public class Main { 5 public static void main(String[] args) { 6 Scanner aScanner = new Scanner(System.in); 7 int n= aScanner.nextInt(); //读第一个 8 long a[] = new long[n]; //存放数组 9 long b[] = n

用双向链表实现大数加减法

写了5个小时,C++面向对象的东西都不会写了...以后要多写C++少写python...关于读入字符串处理的那部分写得太挫就不放了. #include "List.hpp" List 1 #pragma once 2 #include <cstdio> 3 #include <cassert> 4 5 namespace kirai { 6 template<class type> class List { 7 private: 8 typedef