/*
* 解题思路:
* 题意: 简单的大整数相加
* 几个注意点:1、注意输入两个数位数如果不同的位数控制
* 2、最高位加完后可能存在向高一位的进位( 容易忽略 )
*/
#include <stdio.h> #include <string.h> #define A 120 int x,y,in; int len1,len2; char s[ A ]; char sum[ 2*A ]; void calc( int len , int pos ) { int i; 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 main( ) { int i; in = 0; memset( s,‘0‘,sizeof( s ) ); scanf("%s",s ); strcpy( sum , s ); while( scanf("%s",s ) ) { len1 = strlen( s ); len2 = strlen( sum ); if( len1 == 1 && s[ 0 ] == ‘0‘ ) break; 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‘; } 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‘; } len1>=len2 ? calc( len1 , len1-1 ) : calc( len2 , len2-1 ); if( in ) { for( i=len2;i>0;i-- ) sum[ i ] = sum[ i-1 ]; sum[ 0 ] = ‘1‘; in = 0; } } printf("%s\n",sum); return 0; }
原文:http://blog.csdn.net/u011886588/article/details/18862365