当前位置:网站首页>【暑期每日一题】洛谷 P1601 A+B Problem(高精)
【暑期每日一题】洛谷 P1601 A+B Problem(高精)
2022-07-29 07:08:00 【AC_Dragon】
题目链接:P1601 A+B Problem(高精) - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
题目描述
高精度加法,相当于 a+b problem,不用考虑负数。
输入格式
分两行输入。a,b <= 10^500。
输出格式
输出只有一行,代表 a+b 的值。
样例 #1
样例输入 #1
1
1样例输出 #1
2样例 #2
样例输入 #2
1001
9099样例输出 #2
10100AC code:
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
string f(string a,string b)
{
if(a=="0" && b=="0")
{
return "0";
}
int t1=a.size();
int t2=b.size();
int n=abs(t1-t2);
for(int i=0;i<n;i++)
{
if(t1>=t2)
b="0"+b;
else
a="0"+a;
}
string s; // 和
int c=0; // 进位
int d=0; // 低位
int len=a.size()-1;
for(int i=len;i>=0;i--)
{
d=(a[i]-'0'+b[i]-'0')%10+c;
if(d==10)
{
c=1;
s="0"+s;
continue;
}
s=to_string(d)+s;
c=(a[i]-'0'+b[i]-'0')/10;
if(c==1 && i==0)
s="1"+s;
}
if(s[0]=='0')
s="1"+s;
return s;
}
int main()
{
string a,b;
cin>>a>>b;
cout<<f(a,b);
return 0;
}边栏推荐
猜你喜欢

Comparison of advantages between can & canfd integrated test analysis software lkmaster and PCA Explorer 6 analysis software

使用VsCode配置MySQL实现连接、查询、等功能

After three years of outsourcing, the salary of automatic testing after job hopping is twice that of the original. The secret is

【WPF】通过动态/静态资源实现语言切换

利用C语言巧妙实现棋类游戏——三子棋

WPF simple login page completion case

It's enough for MySQL to have this article (disgusting and crazy typing 37k words, just for Bo Jun's praise!!!)

Thoroughly understand kubernetes scheduling framework and plug-ins

作业7.28 文件IO与标准IO

MySQL - multi table query
随机推荐
BeanUtils.setProperty()
logback日志级别简介说明
Vue router route cache
Scala higher order (10): exception handling in Scala
Vagrant box cluster processing
It's enough for MySQL to have this article (disgusting and crazy typing 37k words, just for Bo Jun's praise!!!)
Use vscode to configure Mysql to realize connection, query, and other functions
Scala 高阶(九):Scala中的模式匹配
Amazon cloud assistant applet is coming!
halcon的安装以及在vs2017中测试,vs2017中dll的配置
log4j Layout简介说明
PAT甲级 1150 旅行商问题
cdc source能读完MySqlSnapshotSplit 就退出嘛
Scala higher order (IX): pattern matching in Scala
Full process flow of CMOS chip manufacturing
1 - background project construction
Using C language to skillfully realize the chess game -- Sanzi chess
do end用法的妙处
Gin routing, parameters, output
Homebrew brew update 长时间没反应(或卡在 Updating Homebrew...)