package interview; import java.nio.CharBuffer; public class BigNumberAdd { public static void main(String[] args) { String value1 = "155"; String value2 = "555"; String result = add(value1, value2); String result2 = add2(value1, value2); System.out.println("the scan result is " + result); System.out.println("the scan result2 is " + result2); } public static String add2(String strNumber1, String strNumber2) { int str1length = strNumber1.length(); int str2Length = strNumber2.length(); int max = Math.max(str1length, str2Length); char result[] = new char[max + 1]; for (int i = 0; i < max; ++i) { int lastIndex1 = str1length - 1 - i; int lastIndex2 = str2Length - 1 - i; int resultIndex = result.length - 1 - i; int value; if (result[resultIndex] == 0) { value = 0; } else { value = result[resultIndex] - '0'; } if (strNumber1.charAt(lastIndex1) >= '0' && strNumber1.charAt(lastIndex1) <= '9') { if (lastIndex1 >= 0) { value += strNumber1.charAt(lastIndex1) - '0'; } } else { throw new RuntimeException("error"); } if (strNumber2.charAt(lastIndex1) >= '0' && strNumber1.charAt(lastIndex1) <= '9') { if (lastIndex2 >= 0) { value += strNumber2.charAt(lastIndex2) - '0'; } } else { throw new RuntimeException("error"); } int overFlow = value / 10; value = value % 10; result[resultIndex] = (char) ((char) value + (char) '0'); result[resultIndex - 1] = (char) ((char) overFlow + (char) '0'); } if (result[0] == '0') { return new String(result, 1, result.length - 1); } else { return new String(result); } } public static String add(String strNumber1, String strNumber2) { int max = Math.max(strNumber1.length(), strNumber2.length()); int overFlowValue = 0; char result[] = new char[max + 1]; for (int i = 0; i < max; ++i) { int str1length = strNumber1.length(); int str2Length = strNumber2.length(); int index1 = i - (max - str1length); int index2 = i - (max - str2Length); int intValue1 = 0; int intValue2 = 0; if (index1 >= 0) { intValue1 = strNumber1.charAt(index1) - '0'; } if (index2 >= 0) { intValue2 = strNumber2.charAt(index2) - '0'; } int overFlow = intValue1 + intValue2; for (int j = i; j >= 0; --j) { if (overFlow == 0) { break; } if (result[j + 1] == 0) { result[j + 1] = '0'; } int preValue = result[j + 1] - '0'; preValue = preValue + overFlow; overFlow = preValue / 10; preValue = preValue % 10; result[j + 1] = (char) ((char) preValue + '0'); } if (overFlow != 0) { overFlowValue = overFlowValue + overFlow; } } if (overFlowValue != 0) { result[0] = (char) ((char) overFlowValue + '0'); return String.valueOf(result, 0, result.length); } else { return String.valueOf(result, 1, result.length - 1); } } }
密码管理请下载:
http://a.app.qq.com/o/simple.jsp?pkgname=com.wa505.kf.epassword
相关推荐
本程序由Visual C++ 6.0 开发,可以用于两个一元多项式相加,经编译测试通过,适合于数据结构(C语言版)的初学者。
数据结构实验报告
示例:输入:(2 -> 4 -> 3) + (5 -> 6 -> 4)输出:7 -> 0 -> 8原因:342 + 465 = 807题目解析设立一个表示进位的
该文件夹是用vs2017实现的整个项目文件,作为数据结构的第一次上机实习,用的也只是一些数据结构中较为基础的知识,仅仅用到最简单的链表就能实现大数的相加,该代码仅供参考,代码还是自己动手实现才有意义。
本程序的功能是实现无限大的2个整数相加或者相减,供编程初学者参考
一元多项式相加是通过键盘输入两个形如P0+P1X1+P2X2+…+PnXn 的多项式,经过程序运算后在屏幕上输出他们的相加和。
用两个链表组织两个一元多项式,将相加的结果保存在前一个链表中。 输入 m(项数) c1(第一项系数) e1(第一项指数) c2 e2 ....... cm(第m项系数) em(第m项指数) n(项数) c1(第一项系数) e1(第一项指数) c2 e2 ....
C++ 大整数相加代码 C++ 大整数相加代码 C++ 大整数相加代码
实现了A=7+3x+9x^8+5x^17与B=8x+22x^7+(-9)x^8相加的和,内含算法详细讲解,运行结果与源代码
本文实例为大家分享了Android实现两个数相加的具体代码,供大家参考,具体内容如下 要实现如图所示的加法计算器的话,还是比较简单的,下面直接上demo,有不懂的可以留言交流。 1、下面是activity.xml的布局文件 &...
用JAVA来实现数据结构的一元多项式相加
# 题目:计算两个矩阵相加。 # 程序分析:创建一个新的矩阵,使用 for 迭代并取出 X 和 Y 矩阵中对应位置的值,相加后放到新矩阵的对应位置中。
一元多项式相加的 一个程序 你仔仔 看 啊
大整数相加源代码
c++一元多项式相加源码
参加微软的一道面试题,连续的整数相加等于500的。请用c#写出
数据结构一元多项式相加 用C语言实现的,功能强大好理解。
一元多项式相加减 详细的解释 源代码 运行结果
各位相加给定一个非负整数 num,反复将各个位上的数字相加,直到结果为一位数。示例:输入: 38输出: 2解释: 各位相加的过程为:3 + 8 = 11, 1
本课程设计利用带头结点的单链表求解一元多项式相加的问题,一元多项式的各项都用一个结点保存,每输入一个结点就用头插法插入链表,并将结点按多项式的系数大小排序,相加时只要将指数相同项的系数相加.另外,在单链表...