【模板】大数乘法(51nod 1027)

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #define LL long long
 5 #define rg register
 6 #define N 200010
 7 using namespace std;
 8 int n,m,len,a[N],b[N],t[N];
 9 char s[N],s2[N];
10 inline int read(){
11     int k=0,f=1; char c=getchar();
12     while(c<'0'||c>'9')c=='-'&&(f=-1),c=getchar();
13     while('0'<=c&&c<='9')k=k*10+c-'0',c=getchar();
14     return k*f;
15 }
16 int main(){
17     scanf("%s",s+1); scanf("%s",s2+1);
18     n=strlen(s+1); m=strlen(s2+1); len=n+m+1;
19     if(n<m){
20         for(rg int i=1;i<=m;i++) a[m-i+1]=s2[i]-'0';
21         for(rg int i=1;i<=n;i++) b[n-i+1]=s[i]-'0';
22         swap(n,m);
23     }
24     else{
25         for(rg int i=1;i<=n;i++) a[n-i+1]=s[i]-'0';
26         for(rg int i=1;i<=m;i++) b[m-i+1]=s2[i]-'0';
27     }
28     for(rg int i=1;i<=m;i++){
29         for(rg int j=1;j<=n;j++)
30         t[i+j-1]+=b[i]*a[j];
31     }
32     for(rg int i=1;i<=len;i++){
33         int tmp=t[i]/10;
34         t[i]-=tmp*10;
35         t[i+1]+=tmp;
36     }
37     while(!t[len]) len--;
38     for(rg int i=len;i;i--) printf("%d",t[i]); puts("");
39     return 0;
40 }
View Code

 

posted @ 2018-09-15 20:17  Driver_Lao  阅读(126)  评论(0编辑  收藏  举报