给出2个大整数A,B,计算A*B的结果。
Input
第1行:大数A 第2行:大数B (A,B的长度 <= 1000,A,B >= 0)
Output
输出A * B
Input示例
123456 234567
Output示例
28958703552
#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std;
char a[1000];
char b[1000];
char result[3000];
void fun(int m, int n)
{
int len = 0;
int pos = 0;
memset(result, 0, sizeof(result));
for (int i = m-1; i >= 0; i--)
{
int d = pos;
for (int j = n-1; j >= 0; j--)
{
int temp = a[i] * b[j];
temp += result[d];
result[d] = temp % 10;
result[d+1] += temp / 10;
d++;
}
pos++;
}
bool found = false;
for (int i = m+n+1; i >= 0; i--)
{
if (result[i] != 0)
{
found = true;
cout << (int)result[i];
}
else if (found)
{
cout << (int)result[i];
}
}
}
int main()
{
cin >> a >> b;
int m = strlen(a);
int n = strlen(b);
for (int i = 0; i < m; i++)
{
a[i] -= '0';
}
for (int j = 0; j < n; j++)
{
b[j] -= '0';
}
fun(m, n);
return 0;
}