/*
* 解题思路:
* 题意很好理解、题目也没有陷阱、但是如果想用通俗方法( 不是网上很多就简单几行代码过题写法 )写这道题还是有些难度的、注意点如下
* 1、边界问题、上界是2147483647、自己可以测几组边界数据监测( 2147483647+1 和 2147483647 * 1 )
* 2、内部测试数据可以定有前缀为0的数、这里一定要判断好( 00000000000001 * 00000000000006 )
* 3、第三点我错了很久、先前直接只要两个数、任何一个数溢出、则我判断答案溢出,但是忘记考虑任何数乘以0都为0了!!
*/
#include <stdio.h> #include <string.h> #define A 300 char sample[ 15 ] = "2147483647"; char ss1[ A ],ss2[ A ],c[ 5 ]; char x[ A ]; int in; int Judge( char sss[] ) { int i; for( i=0;i<10;i++ ) { if( sss[ i ]-‘0‘> sample[ i ]-‘0‘ ) return 1; if( sss[ i ]-‘0‘ < sample[ i ]-‘0‘ ) return 0; } return 0; } void calc( int len , int pos ,char s[] , char sum[] ) { int i; int x,y; if( !pos ) return; for( i=pos;i>=0;i-- ) { x = s[ i ] -‘0‘; y = sum[ i ] -‘0‘; if( x+y+in <= 9 && x+y+in >=0 ) { sum[ i ] = ( char)( x+y+in+‘0‘ ); in = 0; } else if( x+y+in > 9) { sum[ i ] = ( char )( (x+y+in)%10 )+‘0‘; in = 1; } } } int Mutiply( char s1[] ,char s2[] , int len1 ,int len2 ) { int x,y,z,zz=0; int i,j,k; int tmp,flag; int len; char ss[ A ],s[ A ]; if( len1 > len2 ) { strcpy( s , s1 ); strcpy( s1,s2 ); strcpy( s2 , s ); tmp = len1; len1 = len2; len2 = tmp; } memset( ss , ‘0‘ , sizeof( ss ) ); for( i=len2-1;i>=0;i-- ) { in = 0; memset( s , ‘0‘, sizeof( s ) ); for( j=len1-1;j>=0;j-- ) { x = s1[ j ] - ‘0‘; y = s2[ i ] - ‘0‘; if( ( x*y+in) >= 10 ) { s[ i+j ] = ( char )(( (x*y+in)%10 )+‘0‘); in = (x*y+in)/10; } else if( (x*y+in )<=9 && (x*y+in ) >=0 ) { s[ i+j ] =(char) (x*y+in +‘0‘); in = 0; } } if( in != 0 && i!=0 ) s[ i-1 ] = in+‘0‘; flag = 0; for( j=i+len2-1;j>=0;j-- ) { if( ss[ j ]-‘0‘ + s[ j ] -‘0‘ + flag >=10 ) { ss[ j ] = (ss[ j ]-‘0‘ + s[ j ] - ‘0‘ + flag )%10+‘0‘; flag = 1; } else { ss[ j ] += s[ j ] -‘0‘+ flag; flag = 0; } } if( i == 0 && in+flag ) { zz = 0; memset( s ,‘\0‘, sizeof( s ) ); z = in + flag; while( z!=0 ) { s[ zz++ ] = z%10+‘0‘; z/=10; } ss[ len1+len2-1+zz ] = ‘\0‘; len = strlen( ss ); for( k=len+zz;k>=zz;k-- ) ss[ k ] = ss[ k-zz ]; for( k=0;k<zz;k++ ) ss[ zz-k-1 ] = s[ k ]; } } ss[ len1+len2-1+zz ] = ‘\0‘; len = strlen( ss ); if( len >10 ) return 1; else if( len <10 ) return 0; else if( len == 10 ) return Judge( ss ); } int Plus( char s[] ,char sum[] , int len1 , int len2 ) { int i; in = 0; if( len1 == 1 && s[ 0 ] == ‘0‘ ) return 0; if( len1 < len2 ) { for(i=len1-1 ; i>=0 ;i-- ) s[ i+len2-len1 ] = s[ i ]; for( i=0;i<len2-len1;i++ ) s[ i ] =‘0‘; sum[ len2 ] = ‘\0‘; } else if( len1 > len2 ) { for( i=len2-1 ; i>=0;i-- ) sum[ i+len1-len2 ] = sum[ i ]; for( i=0;i<len1-len2;i++ ) sum[ i ] = ‘0‘; sum[ len1 ] = ‘\0‘; } len1>=len2 ? calc( len1 , len1-1 , s , sum ) : calc( len2 , len2-1 , s , sum ); if( in ) { for( i=len2;i>0;i-- ) sum[ i ] = sum[ i-1 ]; sum[ 0 ] = ‘1‘; in = 0; } len2 = strlen( sum ); if( len2 > 10 ) return 1; else if( len2 < 10 ) return 0; else if( len2 == 10 ) return Judge( sum ); } int main( ) { int i,j; int flag,vis,sum; int len1,len2; char str[ 5 ][ 30 ] ={"first number too big","second number too big","result too big"}; memset( ss1 , ‘0‘ , sizeof( ss1 ) ); memset( ss2 , ‘0‘ , sizeof( ss2 ) ) ; while( ~scanf("%s%s%s",ss1,c,ss2) ) { flag = vis = sum = 0; len1 = strlen( ss1 ); len2 = strlen( ss2 ); printf("%s %s %s\n",ss1,c,ss2 ); for( i=0;i<len1;i++ ) if(!vis && ss1[ i ] == ‘0‘ ) sum++; else break; if( sum ) { j = 0; for( i=sum;i<len1;i++ ) ss1[ j++ ] = ss1[ i ]; len1 = j; ss1[ j ] = ‘\0‘; } vis = sum =0; for( i=0;i<len2;i++ ) if(!vis && ss2[ i ] == ‘0‘ ) sum++; else break; if( sum ) { j = 0; for( i=sum;i<len2;i++ ) ss2[ j++ ] = ss2[ i ]; len2 = j; ss2[ j ] = ‘\0‘; } if( len1 > 10 || ( len1 == 10 && Judge( ss1 ) ) ) printf("%s\n",str[ 0 ] ); if( len2 > 10 || ( len2 ==10 && Judge( ss2 ) ) ) printf("%s\n",str[ 1 ] ); if( c[ 0 ] == ‘+‘ && len1!=0 && len2 !=0 ) if( Plus( ss1 , ss2 , len1 , len2 ) ) flag = 1; if( c[ 0 ] == ‘*‘ && len1!=0 && len2 !=0 ) if( Mutiply( ss1 , ss2 , len1 , len2 ) ) flag = 1; if( flag ) printf("%s\n",str[ 2 ] ); } return 0; }