大整數乘法
這次的一題又是高精度系列的了。。。先看一下題目:
題目
總Time Limit: 1000ms Memory Limit: 65536kB
Description
求兩個不超過200位的非負整數的積。
Input
有兩行,每行是一個不超過200位的非負整數,沒有多餘的前導0。
Output
一行,即相乘後的結果。結果裡不能有多餘的前導0,即如果結果是342,那麼就不能輸出為0342。
Sample Input
12345678900
98765432100
Sample Output
1219326311126352690000
思路
首先這個絕對不是什麼int,long long,之類的資料類型就可以輕易解決地,肯定要用數組來逐個處理啦。做法就是類似模拟乘法算式,記住,前導0一定要去掉。
AC代碼:
#include<bits/stdc++.h>
using namespace std;
int main(){
char c1[205],c2[205];
cin>>c1>>c2;
int n1[205] = {0},n2[205] = {0},n3[2018] = {0};
int i,z = 0,j;
int len1 = strlen(c1),len2 = strlen(c2);
for(i = 1;i<=len1;i++)n1[i] = c1[len1-i]-48;
for(i = 1;i<=len2;i++)n2[i] = c2[len2-i]-48;
for(i = 1;i<=len1;i++){
z = 0;
for(j = 1;j<=len2;j++){
n3[i+j-1]+=n1[i]*n2[j]+z;
z = n3[i+j-1]/10;
n3[i+j-1]%=10;
}
n3[i+j-1] = z;
}
i = len1+len2;
while(n3[i] == 0&&i>=0)i--;
if(i == -1){
cout<<0;
}
while(i>=1)cout<<n3[i--];
return 0;
}