当前位置:网站首页>【暑期每日一题】洛谷 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;
}边栏推荐
猜你喜欢

以太网接口介绍

一篇长文---深入理解synchronized

Practice of online problem feedback module (XVII): realize the online download function of excel template

Error 1045 (28000) access denied for user 'root' @ 'localhost' solution

How does MySQL convert rows to columns?

Round avatar of user list and follow small blocks

Thoroughly understand kubernetes scheduling framework and plug-ins

WPF nested layout case

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

MySQL如何把行转换为列?
随机推荐
Problems encountered in vmware16 installing virtual machines
作业7.28 文件IO与标准IO
Leetcode buckle classic problem -- 4. Find the median of two positively ordered arrays
亚马逊云助手小程序来啦!
计算程序运行时间 demo
JS 鸡生蛋与蛋生鸡问题,Object与Function究竟谁出现的更早?Function算不算Function的实例?
JS day 4 process control (if statement and switch statement)
MySQL 有这一篇就够(呕心狂敲37k字,只为博君一点赞!!!)
路由中的生命周期钩子 - activated与deactivated
gcc/g++的使用
logback日志级别简介说明
3-global exception handling
20-40k | mecarmand 3D vision algorithm / software / Product Manager Recruitment
暑期总结(二)
logback filter过滤器简介说明
Homebrew brew update 长时间没反应(或卡在 Updating Homebrew...)
Summary of OCR optical character recognition methods
Cvpr2021 | multi view stereo matching based on self supervised learning (cvpr2021)
1-后台项目搭建
Variables and encryption in ansible