华为上机测试题(大数相乘-java)
PS:这个不是自己写的,测试OK,供参考。
/**
* 大数相乘
*/
1 public class BigData { 2 3 public static void main(String[] args) { 4 // TODO Auto-generated method stub 5 StringBuffer sb = new StringBuffer(); 6 // String str1 = "99999999999"; 7 // String str2 = "99999999999"; 8 for (int i = 0; i < 2000; i++) { 9 sb.append(9); 10 } 11 String str1 = sb.toString(); 12 String str2 = str1; 13 BigData test = new BigData(); 14 String result = test.mutiStr(str1, str2); 15 System.out.println(result); 16 } 17 18 public String mutiStr(String str1, String str2) { 19 String result = null;// 存放结果字符串 20 21 // 确定负号 22 int flagStr1 = 0; 23 if (str1.startsWith("-")) { 24 flagStr1++; 25 } 26 int flagStr2 = 0; 27 if (str2.startsWith("-")) { 28 flagStr2++; 29 } 30 // ********************************************************************* 31 32 // 将数字放到整数数组,逆序,字符串尾部对齐 33 int[] arrStr1 = new int[str1.length() - flagStr1]; 34 for (int i = 0; i < arrStr1.length; i++) { 35 arrStr1[i] = str1.charAt(str1.length() - 1 - i) - '0'; 36 } 37 int[] arrStr2 = new int[str2.length() - flagStr2]; 38 for (int i = 0; i < arrStr2.length; i++) { 39 arrStr2[i] = str2.charAt(str2.length() - 1 - i) - '0'; 40 } 41 // *********************************************************************** 42 43 // 运算开始,arrStr2的每位去乘arrStr1的每位 44 StringBuffer subResult = new StringBuffer();// 中间结果 45 for (int i = 0; i < arrStr2.length; i++) { 46 StringBuffer midResult = new StringBuffer();// 每位结果 47 int subBenWei = 0;// 每位相乘时的本位上数字 48 int subJinWei = 0;// 每位相乘时的进位上数字 49 for (int k = 0; k < i; k++) { 50 midResult.append(0);// 先添加0,确保每位末尾对齐,便于加法运算 51 } 52 for (int j = 0; j < arrStr1.length; j++) { 53 subBenWei = (arrStr2[i] * arrStr1[j] + subJinWei) % 10; 54 subJinWei = (arrStr2[i] * arrStr1[j] + subJinWei) / 10; 55 midResult.append(subBenWei); 56 if ((j == arrStr1.length - 1) && subJinWei > 0) { 57 midResult.append(subJinWei);// 如果最前面位相乘有进位,结果添加进位数字 58 } 59 } 60 if (0 == i) { 61 subResult = midResult;// 第一位,子结果存放第一位结果 62 } else { 63 // 各位对齐,确保中间结果和每位结果位数相同,便于相加运算 64 if (midResult.length() > subResult.length()) { 65 int diff = midResult.length() - subResult.length(); 66 while (diff > 0) { 67 subResult.append(0); 68 diff--; 69 } 70 } else { 71 int diff = subResult.length() - midResult.length(); 72 while (diff > 0) { 73 midResult.append(0); 74 diff--; 75 } 76 } 77 // ********************************************************* 78 // 相加运算,结果放到subResult中 79 int benWei = 0; 80 int jinWei = 0; 81 for (int j = 0; j < subResult.length(); j++) { 82 83 benWei = ((subResult.charAt(j) - '0') 84 + (midResult.charAt(j) - '0') + jinWei) % 10; 85 jinWei = ((subResult.charAt(j) - '0') 86 + (midResult.charAt(j) - '0') + jinWei) / 10; 87 subResult.setCharAt(j, (char) (benWei + '0')); 88 } 89 // 如果最后相加任有进位,结果中添加进位 90 if (jinWei > 0) { 91 subResult.append(jinWei); 92 } 93 // *********************************************************** 94 } 95 } 96 // 判断“-”的个数,没有或有两个则为结果为正,不考虑,如只有一个则结果要添加“-” 97 if (1 == (flagStr1 + flagStr2)) { 98 subResult.append('-'); 99 } 100 result = subResult.reverse().toString();// 最终子结果逆序,即为结果 101 return result; 102 } 103 }