C++高精度加法模板

注意点:

1. 用字符串、数组进行储存;
2.注意高精度位数的确定;
3.注意进位借位处理; 
#include 
#include 
#include 
#include 
#include 
#include 
#define N 10001
using namespace std ;
int main ( )
{
    char a1 [ N ] , b1 [ N ] ;
    int a [ N ] , b [ N ] , c [ N ] , i , x = 0 ;
    memset( a , 0 , sizeof ( a ) ) ;
    memset( b , 0 , sizeof ( b ) ) ;
    memset( c , 0 , sizeof ( c ) ) ;
    gets ( a1 ) ;
    gets ( b1 ) ;             // 输入加数与被加数 
    int lena = strlen ( a1 ) ;
    int lenb = strlen ( b1 ) ;
    for ( i = 0 ; i < lena ; i ++ ) a [ lena - i ] = a1 [ i ] - 48 ;
    for ( i = 0 ; i < lenb ; i ++ ) b [ lenb - i ] = b1 [ i ] - 48 ;
    int lenc = 1 ;
    while ( lenc <= lena || lenc <= lenb )
    {
        c [ lenc ] = a [ lenc ] + b [ lenc ] + x ;   //两数相加
        x = c [ lenc ] / 10 ;
        c [ lenc ] %= 10 ;
        lenc ++ ;   
    } 
    c [ lenc ] = x ;
    if ( c [ lenc ] == 0 ) lenc -- ;     //处理最高位
    for ( i = lenc ; i >= 1 ; i -- ) printf ( "%d" , c [ i ] ) ;  //输出结果
    return 0 ;
}

相关链接:

C++模板小站:
https://blog.csdn.net/zj_mrz/article/details/80950647

C++快速幂模板:
https://blog.csdn.net/zj_mrz/article/details/80950616

C++高精度减法模板:
https://blog.csdn.net/zj_mrz/article/details/80965324

C++高精度乘法模板:
https://blog.csdn.net/zj_mrz/article/details/80967394

C++并查集模板:
https://blog.csdn.net/zj_mrz/article/details/81203931

你可能感兴趣的:(信息学竞赛,模板,高精度计算)