大数相乘ADT(String)
string BigNumMultipy( string str1, string str2 ) { int len1 = str1.length(); int len2 = str2.length(); vector< int > res( len1 + len2, 0 ); reverse( str1.begin(), str1.end() ); reverse( str2.begin(), str2.end() ); int i, j, k; for( i = 0; i < len1; i++ ) { for( j = 0; j < len2; j++ ) { res[i+j] += ( str1[i] - '0' ) * ( str2[j] - '0' ); } } for( k = 0; k < len1 + len2; k++ ) { if( res[k] >= 10 ) { res[k+1] += res[k] / 10; //这两句顺序一定要正确,先算进位,然后才是本位。 res[k] %= 10; } } string resStr = ""; for( k = 0; k < len1 + len2; k++ ) { char c = '0' + res[k]; resStr += c; } reverse( resStr.begin(), resStr.end() ); return resStr; }