SDNU 1232.A*B Problem(高精度)
这道题虽然数据很大,但是可以用数学的乘法运算来解决
Description
Calculate a*b
Input
Input contains multiple test cases.
Each test case contains two number a and b (0<=a,b<=10^1000),
Output
Output a*b
Sample Input
12345678987654321 98765432123456789
Sample Output
1219326320073159566072245112635269
Hint
练习模拟大数乘法
#include <cstdio> #include <iostream> #include <cmath> #include <string> #include <cstring> #include <algorithm> using namespace std; string a, b; int x[1000+8], y[1000+8], sum[1000000+8], t; int main() { while(cin >> a >> b) { int lena = a.size(); int lenb = b.size(); int lens = 0; memset(sum, 0, sizeof(sum)); for(int i = 0; i<lena; i++) { x[lena-i] = a[i]-48;//-48等于-'0',并且把这个字符串倒序输入数列X中 } for(int i = 0; i<lenb; i++) { y[lenb-i] = b[i]-48; } for(int i = 1; i<=lena; i++)//像数学乘法一样,把所有的数都乘起来 { t = 0;//记录要进的位数 for(int j = 1; j <= lenb; j++) { sum[i+j-1] += x[i]*y[j]+t; t = sum[i+j-1]/10; sum[i+j-1] %= 10; } sum[i+lenb] = t; } lens = lena+lenb; while(sum[lens] == 0 &&lens>1) lens--; for(int i = lens; i >= 1; i--)printf("%d", sum[i]); printf("\n"); } return 0; }