问题描述
代码示例
#include <bits/stdc++.h>
using namespace std;int main(){char a[64],b[64];cin >> a >> b;int A[64],B[64],C[128];for(int i = 0; i < strlen(a); i++){A[i] = a[i] - '0';}for(int i = 0; i < strlen(b); i++){B[i] = b[i] - '0';}// 反转 for(int i = 0, j = strlen(a) - 1; i < j; i++, j--){int temp = A[i];A[i] = A[j];A[j] = temp;} for(int i = 0, j = strlen(b) - 1; i < j; i++, j--){int temp = B[i];B[i] = B[j];B[j] = temp;} // 初始化 for(int i = 0; i < 128; i++){C[i] = 0;}// 按位相乘int temp;for(int i = 0; i < strlen(a); i++){for(int j = 0; j < strlen(b); j++){C[i + j] += A[i] * B[j];}}for(int i = 0; i <= strlen(a) + strlen(b); i++){if(C[i] > 9){C[i + 1] += C[i] / 10;C[i] %= 10;}}// 输出 if(C[strlen(a) + strlen(b) - 1] == 0){for(int i = strlen(a) + strlen(b) - 2; i >= 0; i--){cout << C[i];}}else{for(int i = strlen(a) + strlen(b) - 1; i >= 0; i--){cout << C[i];}}return 0;
}
结果演示