大数加法
string temp1,temp2,temp,str; string sum(string s1,string s2) { if(s1.length()<s2.length()) { string temp=s1; s1=s2; s2=temp; } int i,j; for(i=s1.length()-1,j=s2.length()-1; i>=0; i--,j--) { s1[i]=char(s1[i]+(j>=0?s2[j]-'0':0)); if(s1[i]-'0'>=10) { s1[i]=char((s1[i]-'0')%10+'0'); if(i) s1[i-1]++; else s1='1'+s1; } } return s1; } string cmp(string aa,string bb) { if(aa.size()>bb.size()) return bb; if(aa.size()<bb.size()) return aa; if(aa<bb) return aa; return bb; }
大数乘法
#include <iostream> #include <cstdio> #include <string> #include <cstring> #include <algorithm> #include <cmath> #define N 30001 using namespace std ; string mul(string a1,string b1) { int lena = a1.size(), lenb = b1.size(),x; int a[N],b[N],c[N]; memset(a,0,sizeof(a)); memset(b,0,sizeof(b)); memset(c,0,sizeof(c)); for ( int i = 0 ; i < lena ; i ++ ) a [ lena - i ] = a1 [ i ] - 48 ; for ( int i = 0 ; i < lenb ; i ++ ) b [ lenb - i ] = b1 [ i ] - 48 ; for ( int i = 0 ; i <= lena ; i ++ ) { x = 0 ; //用来存放进位 for ( int j = 1 ; j <= lenb ; j ++ ) { c [ i + j - 1 ] += a [ i ] * b [ j ] + x ; x = c [ i + j - 1 ] / 10 ; c [ i + j - 1 ] %= 10 ; } c [ i + lenb ] = x ; //进位 } int lenc = lena + lenb ; while ( c [ lenc ] == 0 && lenc > 1 ) lenc -- ; string res; res.clear(); for ( int i = lenc ; i > 0 ; i -- ) res+=('0'+c[i]); return res; } int main() { string x="1",y[55]; for(int i=0; i<5; i++) { for(int j=0; j<=9; j++) { if(i==0) y[i*10+j]='0'+j; else { y[i*10+j]='0'+i; y[i*10+j]+='0'+j; } } } y[50]="50"; for(int i=2; i<=50; i++) x=mul(x,y[i]); cout<<x<<endl; return 0; }