题目大意: 输入 n n 个不大于10241024的数字,求它们的积。
Input I n p u t
3 12 34 56Output O u t p u t
22848思路:
题目上写“不超过1024“,全体以为高精乘单精,30分。。。 模板高精乘,直接上代码。
代码:
#include <cstdio> #include <iostream> #include <string> using namespace std; const int maxn=2400; int a[maxn+1],b[maxn+1],c[maxn+1],n,size,t,m; string s; int main() { scanf("%d",&n); a[1]=1; //初始化 while (n--) { for (m=maxn;m;m--) if (a[m]) break; //求a数组的长度 cin>>s; size=s.size(); for (int i=0;i<=size-1;i++) b[size-i]=(int)s[i]-48; //倒序储存 for (int i=1;i<=m;i++) //序列a { t=0; int j; for (j=1;j<=size;j++) //序列b { c[i+j-1]+=a[i]*b[j]+t; t=c[i+j-1]/10; c[i+j-1]%=10; //高精乘模板 } c[size+i]=t; //进位 } for (int i=1;i<=maxn;i++) //重新储存 { a[i]=c[i]; c[i]=b[i]=0; //清零 } } int i=maxn; while (a[i]==0) i--; for (int j=i;j>=1;j--) printf("%d",a[j]); //倒序输出 return printf("\n")&0; }