Luogu1601 A+B Problem (高精度加法)

2022-10-21,

蒟蒻复习了下高精

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define R(a,b,c) for(register int a = (b); a <= (c); ++ a)
#define nR(a,b,c) for(register int a = (b); a >= (c); -- a)
#define Max(a,b) ((a) > (b) ? (a) : (b))
#define Min(a,b) ((a) < (b) ? (a) : (b))
#define Fill(a,b) memset(a, b, sizeof(a))
#define Swap(a,b) a^=b^=a^=b
#define ll long long
#define ON_DEBUG #ifdef ON_DEBUG #define D_e_Line printf("\n\n----------\n\n")
#define D_e(x) cout << #x << " = " << x << endl
#define Pause() system("pause") #else #define D_e_Line ; #endif struct ios{
template<typename ATP>ios& operator >> (ATP &x){
x = 0; int f = 1; char c;
for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-') f = -1;
while(c >= '0' && c <= '9') x = x * 10 + (c ^ '0'), c = getchar();
x*= f;
return *this;
}
}io;
using namespace std; const int N = 1007; char strA[N],strB[N]; int a[N],b[N],c[N]; inline void BigAdd(char *strA, char *strB){
int lenA = strlen(strA + 1), lenB = strlen(strB + 1);
R(i,1,lenA) a[lenA - i + 1] = strA[i]^'0';
R(i,1,lenB) b[lenB - i + 1] = strB[i]^'0';
int len = Max(lenA, lenB);
R(i,1,len){
c[i] += a[i] + b[i];
while(c[i] >= 10){
++c[i+1];
c[i] -= 10;
}
}
++len;
while(c[len] == 0 && len != 1) --len;
nR(i,len,1)
printf("%d", c[i]);
}
int main(){
scanf("%s%s", strA + 1, strB + 1);
BigAdd(strA, strB);
return 0;
}

Luogu1601 A+B Problem (高精度加法)的相关教程结束。

《Luogu1601 A+B Problem (高精度加法).doc》

下载本文的Word格式文档,以方便收藏与打印。