当前位置:网站首页>【暑期每日一题】洛谷 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;
}边栏推荐
- Gin template
- How much data can a single MySQL table store at most?
- 第7节-程序的编译(预处理操作)+链接
- leetcode力扣经典问题——4.寻找两个正序数组的中位数
- Homebrew brew update 长时间没反应(或卡在 Updating Homebrew...)
- Gin parameter validation
- 7-2 calculate the area and perimeter of a regular pentagon (25 points)
- Scala 高阶(九):Scala中的模式匹配
- Does Flink support sqlserver databases? Get the changes of SQLSERVER database
- Win11 system error: code execution cannot continue because ierutil.dll cannot be found. Reinstalling the program may fix this problem
猜你喜欢
Scala 高阶(十):Scala中的异常处理

QT专题:基础部件(按钮类,布局类,输出类,输入类,容器类)

Cvpr2021 | multi view stereo matching based on self supervised learning (cvpr2021)

Full process flow of CMOS chip manufacturing

MySQL 使用客户端以及SELECT 方式查看 BLOB 类型字段内容总结

Win11 system error: code execution cannot continue because ierutil.dll cannot be found. Reinstalling the program may fix this problem

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

Ansible中的变量及加密

Amazon cloud assistant applet is coming!

2022-07-28: what is the output of the following go language code? A:AA; B:AB; C:BA; D:BB。 package main import ( “fmt“ ) func main() { f
随机推荐
Interface test actual project 03: execute test cases
请问flink支持sqlServer数据库么?获取sqlServer数据库的变化
我想问一下,我flink作业是以upsert-kafka的方式写入数据的,但是我在mysql里面去更
js第四天流程控制(if语句和switch语句)
Leetcode 209. subarray with the smallest length (2022.07.28)
论文阅读 (62):Pointer Networks
How does MySQL convert rows to columns?
How to establish EDI connection with Scania in Scania?
Vite3.0都发布了,你还能卷得动吗(新特性一览)
Gin template
logback简介及引入方法
2022-07-28: what is the output of the following go language code? A:AA; B:AB; C:BA; D:BB。 package main import ( “fmt“ ) func main() { f
WPF nested layout case
leetcode力扣经典问题——4.寻找两个正序数组的中位数
Thinkphp6 realizes database backup
QT topic: basic components (button class, layout class, output class, input class, container class)
路由中的生命周期钩子 - activated与deactivated
Homebrew brew update 长时间没反应(或卡在 Updating Homebrew...)
第7节-程序的编译(预处理操作)+链接
log4j Layout简介说明