当前位置:网站首页>Niuke.com: large number addition
Niuke.com: large number addition
2022-06-21 17:20:00 【lsgoose】

It is essentially a simulation of arithmetic :
When we calculate, we must first align the lowest order , Then add them bit by bit , In addition, only one digit is added at a time , Record this carry , Add it to the next time .
The code is as follows :
class Solution {
public:
/**
* The class name in the code 、 Method name 、 The parameter name has been specified , Do not modify , Return the value specified by the method directly
* Calculate the sum of two numbers
* @param s string character string Represents the first integer
* @param t string character string Represents the second integer
* @return string character string
*/
string solve(string s, string t) {
if(s=="") return t;
if(t=="") return s;
int carry=0;
// Ensure the length of the preceding string is long
if(s.length()<t.length()){
swap(s, t);
}
for(int i=s.length()-1;i>=0;i--){
int sum=s[i]-'0'+carry;
// Find the corresponding bit in a shorter string t Position in
int j=(t.length()-s.length())+i;
if(j>=0){
sum+=t[j]-'0';
}
carry=sum/10;
sum=sum%10;
s[i]='0'+sum;
}
if(carry){
s='1'+s;
}
return s;
}
};边栏推荐
- Overseas new things | zoovu, an American AI startup, raised a new round of financing of US $169million to optimize the online "product discovery" experience for consumers
- Undefined functions or variables [explained in one article] (matlab)
- Disruptor本地线程队列_WorkProcessor异常_FatalExceptionHandler---线程间通信工作笔记004
- Qt 知识:使用 QGraphicsPixmapItem类
- Overseas new things | software developer "dynaboard" seed round raised US $6.6 million to develop low code platform to connect design, products and developers
- 实战---商场登录测试
- 撰写有效帮助文档的7大秘诀
- Hairui technology completed the pre-A round of financing of tens of millions of yuan to build the first artificial intelligent distribution Internet of things in China
- 阿里云服务器+宝塔面板+无域名部署web项目
- Google Earth engine (GEE) - use sentinel-2 data acquisition to obtain the NDVI difference of one month ago (Guatemala as an example)
猜你喜欢

Design and implementation of face verification system for floating population management

Unittest框架的测试日志

Previous installation records

Exness: the impact of inflation in the United States is too great, and the leaders of the Federal Reserve have expressed their position one after another

牛客网:大数加法

Overseas new things | zoovu, an American AI startup, raised a new round of financing of US $169million to optimize the online "product discovery" experience for consumers

Use picgo core and Alibaba cloud to automatically upload typera pictures

PowerPoint 教程,如何在 PowerPoint 中更改页面方向、幻灯片大小?

Qt 知识:使用 QGraphicsPixmapItem类

Necessary for data analysis: 6 steps +5 types +2 analysis methods
随机推荐
设计一个打印整棵树的打印函数
【SQLite】解决unrecognized token:“‘“
Circular of the State Council on regulating the management of the rental of housing with common property rights (for Trial Implementation)
[theory] - interface test
《网络是怎么样连接的》读书笔记 - FTTH
d改进翻译
gp中的decode函数实现
The beta version of move protocol is stable, and it is temporarily decided to expand the scale of the prize pool
MATLAB实现的基于对称TSP问题研究
Pytest-- generate test report
期货农产品开户怎么开?手续费是多少?
建立自己的网站(11)
既不是研发顶尖高手,也不是销售大牛,为何偏偏获得 2 万 RMB 的首个涛思文化奖?
新增Razor组件支持代理连接RDP,JumpServer堡垒机v2.23.0发布
The "learning link" database of the learning software is suspected to have leaked information, revealing more than 100million pieces of student information
【mysql学习笔记19】多表查询
【Leetcode】297. 二叉树的序列化与反序列化 (困难)
D improve translation
Pytest framework
Previous installation records