前言
今天朋友给我发了一个面试题,让我想起了ACM的练习题,用C语言实现大数字的加法运算得出结果。这个面试题针对于java来计算两个字符串的和,不能转成int类型计算,也不能使用包装类计算。这两个字符串都只包含0到9的数字,并且都为正数。
两个正整数相加的思路
首先考虑到需要一个变量存储结果,可以用字符数组存储相加后的结果。也考虑到有进位的情况,需要预留一位,以防止进位。
两个正整数相加的实现
根据题,实现的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
| private static String javaAdd1(String num1, String num2) { while (true) { if(num1.length() == num2.length()) { break; }else { if(num1.length() < num2.length()) { num1 = "0" + num1; }else { num2 = "0" + num2; } } } char[] num1Char = num1.toCharArray(); char[] num2Char = num2.toCharArray(); int length = num1.length(); char[] num = new char[length+1]; for(int i=0;i<length+1;i++) { num[i] = '0'; }
for(int i = length-1;i >= 0;i--) { char num1_i = num1Char[i]; char num2_i = num2Char[i]; int addResult = 0; int index = i+1; addResult = (num1_i + num2_i) - ('0' * 2); if(num[index] >'0') { addResult += 1; } if (addResult > 9) { num[index-1] = '1'; addResult = addResult -10; } num[index] = (char) (addResult+'0'); } int index = 0; for(int i=0;i<num.length;i++) { if(num[i] == '0') { index = i+1; }else { break; } } String s = ""; for(int i=index;i<num.length;i++) { s = s+ num[i]; } return s; }
|
可以对其进行优化,优化之后:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| private static String javaAdd2(String num1, String num2) { int lengthNum = num1.length() - num2.length(); StringBuilder zero = new StringBuilder(); int abs = Math.abs(lengthNum); for(int i = 0;i<abs;i++) { zero.append('0'); } if (lengthNum > 0) { num2 = zero.append(num2).toString(); } else { num1 = zero.append(num1).toString(); } char[] num1Char = num1.toCharArray(); char[] num2Char = num2.toCharArray(); int length = num1.length(); char[] num = new char[length+1]; for(int i=0;i<length+1;i++) { num[i] = '0'; } for(int i = length-1;i >= 0;i--) { char num1_i = num1Char[i]; char num2_i = num2Char[i]; int addResult = 0; int index = i+1; addResult = (num1_i + num2_i) - ('0' * 2); if(num[index] >'0') { addResult += 1; } if (addResult > 9) { num[index-1] = '1'; addResult = addResult -10; } num[index] = (char) (addResult+'0'); } String s = new String(num); s = s.replaceAll("^(0+)", ""); return s; }
|
最近在学汇编,汇编用一个寄存器来存储进位,那么我们也可以用一个变量来存储进位的结果。
0代表无进位,1代表有进位。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| private static String javaAdd3(String num1, String num2) { int lengthNum = num1.length() - num2.length(); StringBuilder zero = new StringBuilder(); int abs = Math.abs(lengthNum); for(int i = 0;i<abs;i++) { zero.append('0'); } if (lengthNum > 0) { num2 = zero.append(num2).toString(); } else { num1 = zero.append(num1).toString(); } char[] num1Char = num1.toCharArray(); char[] num2Char = num2.toCharArray(); int length = num1.length(); char[] num = new char[length+1]; for(int i=0;i<length+1;i++) { num[i] = '0'; } int flag = 0; for(int i = length-1;i >= 0;i--) { char num1_i = num1Char[i]; char num2_i = num2Char[i]; int addResult = 0; int index = i+1; addResult = (num1_i + num2_i) - ('0' * 2); if(flag == 1) { addResult += 1; flag = 0; } if (addResult > 9) { addResult = addResult -10; flag = 1; } num[index] = (char) (addResult+'0'); } if(flag == 1) { num[0] = '1'; } String s = new String(num); s = s.replaceAll("^(0+)", ""); return s; }
|
结尾
这个实现也可以移植到C语言上,用来实现大数字的加法运算。