解题思路:
哎,我太笨啦。
逆序字符串 → 乘积 → 进位 → 反转存在字符串里 → 返回字符串。
大佬教教我简单的方法
参考代码:
#include<bits/stdc++.h> using namespace std; void change(char num[], int length) { for (int i = 0; i < length / 2; i++) { char temp = num[i]; num[i] = num[length - i - 1]; num[length - i - 1] = temp; } } char *multiply(char str1[], char str2[]) { int length1 = strlen(str1), length2 = strlen(str2); change(str1, length1); change(str2, length2); int *product = new int[length1 + length2](); char *poi = new char[length1 + length2 + 1](); for (int i1 = 0; i1 < length1; i1++) for (int i2 = 0; i2 < length2; i2++) product[i1 + i2] += (str1[i1] - '0')*(str2[i2] - '0'); for (int i = 0; i < length1 + length2; i++) if (product[i] > 9) { product[i + 1] += product[i] / 10; product[i] = product[i] % 10; } int pos = length1 + length2 - 1; while (pos >= 0 && product[pos] == 0) pos--; if (pos < 0) *poi = '0'; else for (int i = 0; i <= pos; i++) poi[pos - i] = product[i] + '0'; delete[] product; return poi; } int main() { char num1[10001], num2[10001]; cin >> num1 >> num2; char *poi = multiply(num1, num2); puts(poi); delete[] poi; return 0; }
0.0分
5 人评分