您的位置:首页 > 其它

ECNU-ACM-Problem A+B(Big Integer)

2009-07-11 23:05 260 查看
Description


Give two positive integer A and B,calucate A+B.

Notice that A,B is no more than 500 digits.

Input


The test case contain several lines.Each line contains two positive integer A and B.

Output


For each input line,output a line contain A+B

Sample Input


2 3

1231231231823192 123123123123123

1000000000000000 1

Sample Output


5

1354354354946315

1000000000000001

------------------------------------------------------------------------------------------------

Source Code :

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define max 1002
char a[max] ;
char b[max] ;
void reserve_string( char t[max] )
{
int l = strlen( t ) , i ;
char tt ;
for ( i = 0 ; i <= l / 2 && i < ( l- 1 - i )  ; ++ i )
{
tt = t[i] ;
t[i] = t[ l - 1 - i ] ;
t[ l - 1 - i ] = tt ;
}
}
int main()
{
int i = 0 ;
while( scanf("%s %s", a ,b )!= EOF )
{
reserve_string( a ) ;
reserve_string( b ) ;
for ( i = 0 ; a[i] != '/0' || b[i] != '/0' ; ++ i )
{
if( b[i] == '/0' )
{
b[i+1] = '/0' ;
b[i] = '0' ;
}
if ( a[i] == '/0' )
{
a[i+1] = '/0' ;
a[i] = '0';
}
a[i] = a[i] + b[i] - '0' ;
if ( a[i] > '9' )
{
if ( a[i+1] == '/0' )
{
a[i+2] = '/0' ;
a[i+1] = '0' ;
}
a[i+1] += (int)(a[i] - '0') / 10 ;
a[i] = (int)(a[i] - '0') % 10 + '0' ;
}
}
reserve_string( a ) ;
printf("%s/n", a) ;
}
return 0 ;
}

Problems:

1、忘了把字符串要进行转置。

2、在写转 置函数的时候,没有写正确

//Error code
void reserve_string( char t[max] )
{
int l = strlen( t ) , i ;
char tt ;
for ( i = 0 ; i <= l / 2 ; ++ i )
{
tt = t[i] ;
t[i] = t[ l - 1 - i ] ;
t[ l - 1 - i ] = tt ;
}
}

在for 循环中,少了&& i < ( l- 1 - i ) , 导致会出现问题。实际上,在要进行交换的时候,应该是要交换的前一个的索引要比后一个小才进行,所以要加上这个条件。

3、少了对于a[i] = '/0' ;的处理。

应该加上如下代码:

if ( a[i] == '/0' )
{
a[i+1] = '/0' ;
a[i] = '0';
}

否则,在

a = 1 ;

b = 9999 ;

会出现问题。

编程水平下降了,唉!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: