当前位置:网站首页>Haut OJ 1347: addition of choice -- high progress addition
Haut OJ 1347: addition of choice -- high progress addition
2022-07-05 05:17:00 【hunziHang】
Problem description :
It's getting colder and colder in winter ,Choice Of course, I don't want to make everyone cooler , So she gives two integers A,B, You just have to calculate A+B The sum of .
Input :
Multiple groups of input data .
Enter two integers per row A,B
A and B The length of is less than 1000(A,B It's all nonnegative )
Output :
Output A+B Result
The sample input :
1 1 1 2
Sample output :
2 3
Cause analysis :
High progress Algorithm :
1. First create two int Array , And two strings ( Or two char Type array ),int Array initialization , Take the first cell record of the array Data length , And string or char Number in array Single Deposit in int In the array ( Deposit it backwards )
2. find The maximum length of two numbers , then +1( It is only possible to add 1 position , Because the maximum number of rounding is one )
Add two arrays , take i The numbers on the unit are stored in i and i+1 In the unit ( a[i+1] + =a[i]/10;)
3. Remove the leading 0, Because it used to be upside down , So from the len Start Gradually decrease 1
Solution :
#include<bits/stdc++.h>
using namespace std;
#define endl "\n"
int main(void)
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
string str1,str2;
while(cin>>str1>>str2)
{
int a[2000],b[2000],i,j,len;
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
a[0]=str1.length();
b[0]=str2.length();
for(i=1;i<=a[0];i++)
a[i]=str1[a[0]-i]-'0';
for(i=1;i<=b[0];i++)
b[i]=str2[b[0]-i]-'0';
len=max(a[0],b[0])+1;
for(i=1;i<=len;i++)
{
a[i]+=b[i];
a[i+1]+=a[i]/10;
a[i]%=10;
}
while(len>1&&a[len]==0)
len--;
for(i=len;i>=1;i--)
cout<<a[i];
cout<<endl;
}
return 0;
}
边栏推荐
- xftp7与xshell7下载(官网)
- 服务熔断 Hystrix
- [depth first search] 695 Maximum area of the island
- [paper notes] multi goal reinforcement learning: challenging robotics environments and request for research
- Basic knowledge points
- Lua GBK and UTF8 turn to each other
- C4D simple cloth (version above R21)
- 【ES实战】ES上的native realm安全方式使用
- Django reports an error when connecting to the database. What is the reason
- Simple HelloWorld color change
猜你喜欢
[turn to] MySQL operation practice (I): Keywords & functions
UE 虚幻引擎,项目结构
To the distance we have been looking for -- film review of "flying house journey"
2021-10-29
远程升级怕截胡?详解FOTA安全升级
stm32Cubemx(8):RTC和RTC唤醒中断
Redis has four methods for checking big keys, which are necessary for optimization
Research on the value of background repeat of background tiling
Binary search basis
嵌入式数据库开发编程(五)——DQL
随机推荐
Solon Logging 插件的添加器级别控制和日志器的级别控制
[turn]: Apache Felix framework configuration properties
[转]:Apache Felix Framework配置属性
Leetcode word search (backtracking method)
【论文笔记】Multi-Goal Reinforcement Learning: Challenging Robotics Environments and Request for Research
Pause and resume of cocos2dx Lua scenario
小程序直播+電商,想做新零售電商就用它吧!
A three-dimensional button
Binary search basis
Cocos2dx screen adaptation
Database under unity
PMP考生,请查收7月PMP考试注意事项
Dotween usage records ----- appendinterval, appendcallback
UE 虚幻引擎,项目结构
C iterator
JVM call not used once in ten years
room数据库的使用
The next key of win generates the timestamp file of the current day
被舆论盯上的蔚来,何时再次“起高楼”?
Animation