原题链接:
https://vjudge.net/problem/UVA-10106
AC代码:
#include <bits/stdc++.h>
using namespace std;
int main()
{
string s;
int a[300] = {0};
int b[300] = {0};
while (cin >> s)
{
int ans[600] = {0};
int len1 = s.length();
int k = 0;
for (int i = len1 - 1; i >= 0; i--)
{
a[len1 - i - 1] = s[i] - '0';
}
cin >> s;
int len2 = s.length();
for (int i = len2 - 1; i >= 0; i--)
{
b[len2 - i - 1] = s[i] - '0';
}
for (int i = 0; i < len1; i++)
{
for (int j = 0; j < len2; j++)
{
ans[i + j] += a[i] * b[j];
}
}
for (int i = 0; i < len1 + len2; i++)
{
if (ans[i] > 9)
{
ans[i + 1] += ans[i] / 10;
ans[i] = ans[i] % 10;
}
}
int cnt = len1 + len2;
while (!ans[cnt] && cnt >= 1)
{
cnt--;
}
for (int i = cnt; i >= 0; i--)
cout << ans[i];
cout << endl;
}
}
模拟乘法,这种题可以写个大整数类BigInteger模板放上面:
mian函数三行就ac了哈哈哈啊哈