首页 技术 正文
技术 2022年11月10日
0 收藏 759 点赞 2,289 浏览 858 个字

JDOJ 1789: 高精度A+B

JDOJ传送门

洛谷 P1601 A+B Problem(高精)

洛谷传送门

Description

已知两个整数A、B

求A+B

Input

第一行为A

第二行为B

Output

输出一行为A+B的结果

Sample Input

5 6

Sample Output

11

HINT

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

题解:

100000位的大整数加法,不考虑负数。

一道高精度的板子题。

所谓高精度其实就是代码模拟加法的竖式运算,对于这样一道板子题来讲,我简单说一下高精度加法的实现过程:

字符串读入

字符串转数字,从后往前转

模拟竖式运算进行加法。

去除前导0,进行输出。

聪明一点的看代码就能看懂什么意思:

#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;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,090
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,567
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,415
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,187
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,824
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,907