JDOJ 1789: precision A + B

JDOJ 1789: precision A + B

JDOJ Portal

Luo Valley P1601 A + B Problem (high precision)

Luo Gu Portal

Description

Known two integers A, B

Seeking A + B

Input

A first behavior

The second line B

Output

The output A + B of a behavior

Sample Input

5 6

Sample Output

11

HINT

0 <= A, B <= \(10^{100000}\)

answer:

100 000 large integer addition, do not consider negative.

A high-precision board title.

The so-called high-precision analog addition of the code is actually a vertical operation, the board for such a question is concerned, I would like to explain the process to achieve high-precision additions:

String read

String-to-digital, transferred from the back

An analog adder for vertical operation.

Removing the leading 0 is outputted.

Clever little look at the code be able to understand what it means:

#include<cstdio>
#include<cstring>
#include<algorithm>
#pragma GCC optimize(1)
#pragma GCC optimize(2)
#pragma GCC optimize(3)
using namespace std;
const int maxx=1e5+1;
const int INF=1e5;
int a[maxx],b[maxx];
char aa[maxx],bb[maxx];
int main()
{
    scanf("%s%s",aa+1,bb+1);
    int lena=strlen(aa+1);
    int lenb=strlen(bb+1);
    for(int i=1;i<=lena;i++)
        a[i]=aa[lena-i+1]-'0';
    for(int i=1;i<=lenb;i++)
        b[i]=bb[lenb-i+1]-'0';
    int lenc=max(lena,lenb);
    for(int i=1;i<=lenc;i++)
    {
        a[i]+=b[i];
        a[i+1]+=a[i]/10;
        a[i]%=10;
    }
    int t=INF;
    while(!a[t])
    {
        t--;
        if(t==0)
        {
            puts("0");
            return 0;
        }
    }
    for(int i=t;i>=1;i--)
        printf("%d",a[i]);
    return 0;
}

Guess you like

Origin www.cnblogs.com/fusiwei/p/11597394.html