HDU 1402 A * B Problem Plus
这是大数相乘的一模板题;
先把两个字符串变成ASCII码(不能和倒置放在一块,否则会出错),然后把他们全部倒置,然后再将他们相乘,最后再进位,不过这样会超时,将会在近段时间内发布不超时的代码
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define Max 100000
int num[Max];
char str1[Max] = {0},str2[Max] = {0};
void cal( )
{
int len1 = strlen( str1 ),len2 = strlen( str2 );
for( int i = 0; i < ( len1 > len2 ? len1 : len2 ); ++i )
str1[i] -= '0',str2[i] -= '0';
for( int p = 0,q = len1 - 1; q > p; --q,++p )
{
char c = str1[q];
str1[q] = str1[p];
str1[p] = c;
}
for( int p = 0, q = len2 - 1; q > p; --q,++p )
{
char c = str2[q];
str2[q] = str2[p];
str2[p] = c;
}
for( int i = 0; i < len1 ; ++i )
for( int j = 0; j < len2; ++j )
num[i + j] = str1[i] * str2[j];
int i = Max;
while( !num[--i] );
for( int j = 0,c = 0; j <= i||c; ++j )
{
c += num[j];
num[j] = c % 10;
c /= 10;
}
}
int main( )
{
while( scanf( "%s%s",str1,str2 ) != EOF )
{
memset( num,0,sizeof( num ) );
cal( );
int n = Max;
while( !num[--n] );
while( n >= 0 )
printf( "%d",num[n--] );
puts( "" );
memset( str1,0,sizeof( str1 ) );
memset( str2,0,sizeof( str2 ) );
}
return 0;
}
本人还是新手 ,转载请注明来自Lvsi‘s home
浙公网安备 33010602011771号