当前位置:网站首页>【暑期每日一题】洛谷 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;
}边栏推荐
- Clock tree synthesis (I)
- Docker最新超详细教程——Docker创建运行MySQL并挂载
- Nodejs安装教程
- 能在SQL 语句中 指定 内存参数吗?
- PAT甲级 1150 旅行商问题
- Some learning and understanding of vintage analysis
- Thoroughly understand kubernetes scheduling framework and plug-ins
- 路由中的生命周期钩子 - activated与deactivated
- 0 9 布隆过滤器(Bloom Filter)
- Thinkphp6 realizes database backup
猜你喜欢

QT basic day 2 (2) QT basic components: button class, layout class, output class, input class, container and other individual examples

Redis Basics

Using C language to skillfully realize the chess game -- Sanzi chess

halcon的安装以及在vs2017中测试,vs2017中dll的配置

Why does ETL often become ELT or even let?

Full process flow of CMOS chip manufacturing

Synchronous / asynchronous, blocking / non blocking and IO

Ethernet interface introduction

【OpenGL】着色器(Shader)的使用

MySQL 有这一篇就够(呕心狂敲37k字,只为博君一点赞!!!)
随机推荐
I'd like to ask, my flick job writes data in the way of upsert Kafka, but I'm more careful in MySQL
Introduction to logback filter
利用C语言巧妙实现棋类游戏——三子棋
[redis] redis development specifications and precautions
JS break and continue and return keywords
MySQL advanced (Advanced) SQL statement (I)
Section 7 - compilation of programs (preprocessing operations) + links
Variables and encryption in ansible
LevelFilter简介说明
20-40k | mecarmand 3D vision algorithm / software / Product Manager Recruitment
Personal blog system (with source code)
彻底搞懂kubernetes调度框架与插件
WPF simple login page completion case
请问flink支持sqlServer数据库么?获取sqlServer数据库的变化
Leetcode 209. subarray with the smallest length (2022.07.28)
Homebrew brew update 长时间没反应(或卡在 Updating Homebrew...)
Spark Learning Notes (VII) -- spark core core programming - RDD serialization / dependency / persistence / partition / accumulator / broadcast variables
Tp6 use protobuf
When NPM is installed, it is stuck. There are five solutions
SpingBoot整合Quartz框架实现动态定时任务(支持实时增删改查任务)