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 :
Problems:
1、忘了把字符串要进行转置。
2、在写转 置函数的时候,没有写正确
在for 循环中,少了&& i < ( l- 1 - i ) , 导致会出现问题。实际上,在要进行交换的时候,应该是要交换的前一个的索引要比后一个小才进行,所以要加上这个条件。
3、少了对于a[i] = '/0' ;的处理。
应该加上如下代码:
否则,在
a = 1 ;
b = 9999 ;
会出现问题。
编程水平下降了,唉!
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 ;
会出现问题。
编程水平下降了,唉!
相关文章推荐
- http://acm.hdu.edu.cn/showproblem.php?pid=1108 java BIgInteger gcd
- ACM Java 大数之BigInteger详解
- Java中的BigInteger在ACM中的应用
- A + B Problem (Big integer version) 【高精度】
- A + B Problem (Big integer versi…
- Problem A+B(Big Integer)
- A * B Problem (Big integer versi…
- A * B Problem (Big integer version) 【高精度】
- Problem A+B(Big Integer)大数相加
- A * B Problem (Big integer version) 【高精度】
- JAVA大数处理(BigInteger,BigDecimal)
- 使用BigInteger做权限判断
- java.math.BigInteger
- JavaSE8基础 BigInteger 使用递归函数来计算阶乘
- 西南交通大学第十三届ACM决赛 D.Music Problem 背包DP
- 西南交通大学第十三届ACM决赛 D.Music Problem 背包DP
- 西南交通大学第十三届ACM决赛 D.Music Problem 背包DP
- 【codeforces】2014-2015ACM-ICPC CERC 14 Problem G: Virus synthesis 【Palindromic Tree+DP】
- java中将BigInteger转换成int
- Java大数-BigInteger BigDecimal