面试题1:实现大数加减乘除四则运算
通过c++字符串string实现大数的加、减、乘、除
1 string addTwoString(string s1, string s2) { 2 char carry = '0'; 3 string res; 4 int l1 = s1.length(); 5 int l2 = s2.length(); 6 7 int n = max(l1, l2); 8 for (int i = 0; i <= n; i++) { 9 char a = '0'; 10 char b = '0'; 11 if (l1 - i - 1 >= 0) { 12 a = s1[l1 - i - 1]; 13 } 14 if (l2 - i - 1 >= 0) { 15 b = s2[l2 - i - 1]; 16 } 17 int sum = a - '0' + b - '0' + carry - '0'; 18 carry = sum / 10 + '0'; 19 res.insert(res.begin(), sum % 10 + '0'); 20 } 21 22 while (*res.begin() == '0') { 23 res.erase(res.begin()); 24 } 25 return res; 26 } 27 28 string multiplyTwoString(string s1,string s2){ 29 int l1 = s1.length(); 30 int l2 = s2.length(); 31 if(l1 > l2) return multiplyTwoString(s2,s1); 32 33 string res = "0"; 34 for(int i=0;i<l1;i++){ 35 char carry = '0'; 36 string tmp; 37 for(int j=0;j<l2;j++){ 38 int sum = (s1[l1-i-1]- '0') * (s2[l2-j-1] - '0') + carry - '0'; 39 carry = sum / 10 + '0'; 40 tmp.insert(tmp.begin(),sum%10+'0'); 41 } 42 for(int j=0;j<i;j++){ 43 tmp.push_back('0'); 44 } 45 res = addTwoString(res, tmp); 46 } 47 return res; 48 }
1 #include <iostream> 3 #include <cstdlib> 4 #include <vector> 6 #include <sstream> 7 #include <algorithm> 9 using namespace std; 10 11 class Solution { 12 public: 13 string addTwoString(string nums1, string nums2) { 14 int n1 = nums1.size(); 15 int n2 = nums2.size(); 16 //便于操作,统一转换为长数加短数 17 if (n1 < n2) 18 return addTwoString(nums2, nums1); 19 20 int minlen = n2; 21 int maxlen = n1; 22 string result; 23 int carry = 0; 24 for (int i = 0; i < minlen; i++) { 25 int sum = nums1[--n1] - '0' + nums2[--n2] - '0' + carry; 26 carry = sum / 10; 27 int a = sum % 10; 28 result.push_back(a + '0'); 29 } 30 for (int i = minlen; i < maxlen; i++) { 31 int sum = nums1[--n1] - '0' + carry; 32 carry = sum / 10; 33 int a = sum % 10; 34 result.push_back(a + '0'); 35 } 36 37 if (carry != 0) 38 result.push_back(carry + '0'); 39 40 reverse(result.begin(), result.end()); 41 return result; 42 } 43 45 string multiplyTwoString(string nums1, string nums2) { 46 string result = "0"; 47 if (nums1 == "0" || nums2 == "0") 48 return result; 49 50 vector<string> tmpResult; 51 int n1 = nums1.size(); 52 int n2 = nums2.size(); 53 54 int carry = 0; 55 reverse(nums1.begin(), nums1.end()); 56 reverse(nums2.begin(), nums2.end()); 57 58 for (int i = 0; i < n1; i++) { 59 string str; 60 for (int j = 0; j < n2; j++) { 61 int sum = (nums1[i] - '0') * (nums2[j] - '0') + carry; 62 carry = sum / 10; 63 int a = sum - carry * 10; 64 str.push_back(a + '0'); 65 } 66 if (carry != 0) 67 str.push_back(carry + '0'); 68 carry = 0; 69 reverse(str.begin(), str.end()); 70 for (int k = 0; k < i; k++) { 71 str.push_back('0'); 72 } 73 tmpResult.push_back(str); 74 } 75 76 int size = tmpResult.size(); 77 78 for (int i = 0; i < size; i++) { 79 result = addTwoString(result, tmpResult[i]); 80 } 81 return result; 82 } 83 84 string substractTwoString(string nums1, string nums2) { 85 string result; 86 int n1 = nums1.size(); 87 int n2 = nums2.size(); 88 89 //判断符号为正负 90 char sign = '+'; 91 if (n1 < n2) { 92 sign = '-'; 93 nums1.swap(nums2); 94 95 } else if (n1 == n2) { 96 for (int i = 0; i < n1; i++) { 97 if (nums1[i] > nums2[i]) { 98 break; 99 } else if (nums1[i] < nums2[i]) { 100 sign = '-'; 101 nums1.swap(nums2); 102 break; 103 } 104 } 105 } 106 int borrow = 0; 107 reverse(nums1.begin(), nums1.end()); 108 reverse(nums2.begin(), nums2.end()); 109 110 n1 = nums1.size(); 111 n2 = nums2.size(); 112 113 for (int i = 0; i < n2; i++) { 114 int r = nums1[i] - nums2[i] - borrow; 115 borrow = 0; 116 if (r < 0) { 117 r = r + 10; 118 borrow = 1; 119 } 120 result.push_back(r + '0'); 121 } 122 123 for (int i = n2; i < n1; i++) { 124 int r = nums1[i] - '0' - borrow; 125 borrow = 0; 126 if (r < 0) { 127 r = r + 10; 128 borrow = 1; 129 } 130 result.push_back(r + '0'); 131 } 132 133 for (int i = n1 - 1; i >= 0; i--) { 134 if (result[i] == '0') 135 result.erase(result.begin() + i); 136 else 137 break; 138 } 139 reverse(result.begin(), result.end()); 140 if (sign == '-') { 141 result.insert(result.begin(), '-'); 142 } 143 return result; 144 } 145 146 147 string divideTwoString(string nums1, string nums2) { 148 string result; 149 result = substractTwoString(nums1, nums2); 150 if (result[0] == '-') { 151 result = "0"; 152 return result; 153 } 154 result = ""; 155 int n1 = nums1.size(); 156 157 stringstream stream; 158 string dividend; 159 160 for (int j = 0; j < n1; j++) { 161 dividend.push_back(nums1[j]); //被除数 162 //从9到0,试探性的找商,当余数不为负数时,为当前位的值 163 for (int i = 9; i >= 0; i--) { 164 stream.str(""); 165 stream << i; 166 string s = stream.str(); 167 string sj = multiplyTwoString(nums2, s); 168 string remain = substractTwoString(dividend, sj); 169 170 if(remain[0] != '-'){ 171 result.push_back(i+'0'); 172 dividend = remain; 173 break; 174 } 175 } 176 } 177 //去掉商前面的零位 178 int size = result.size(); 179 for(int i=0;i<size;i++){ 180 if(result[0]=='0'){ 181 result.erase(result.begin()); 182 }else { 183 break; 184 } 185 } 186 //四舍五入 187 string s = substractTwoString(multiplyTwoString(dividend,"2"),nums2); 188 if(s[0] == '-') addTwoString(result,"1"); 189 190 return result; 191 } 192 }; 193 194 int main() { 196 string nums1 = "123456"; 197 string nums2 = "789"; 199 Solution s; 201 cout << s.addTwoString(nums1, nums2) << endl; 202 cout << s.multiplyTwoString(nums1, nums2) << endl; 203 cout << s.substractTwoString(nums1, nums2) << endl; 204 cout << s.divideTwoString(nums1, nums2) << endl; 206 return 0; 207 }