当前位置:网站首页>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;
}
边栏推荐
- 2021-10-29
- 服务熔断 Hystrix
- xftp7与xshell7下载(官网)
- BUUCTF MISC
- Generate filled text and pictures
- The present is a gift from heaven -- a film review of the journey of the soul
- Lua wechat avatar URL
- Three dimensional dice realize 3D cool rotation effect (with complete source code) (with animation code)
- Bubble sort summary
- C language Essay 1
猜你喜欢
[paper notes] multi goal reinforcement learning: challenging robotics environments and request for research
2021-10-29
[转]MySQL操作实战(一):关键字 & 函数
Recherche de mots pour leetcode (solution rétrospective)
[interval problem] 435 Non overlapping interval
Binary search basis
对象的序列化
Optimization scheme of win10 virtual machine cluster
2022/7/2 question summary
Grail layout and double wing layout
随机推荐
Download and use of font icons
Unity parallax infinite scrolling background
Establish cloth effect in 10 seconds
[转]MySQL操作实战(一):关键字 & 函数
Recherche de mots pour leetcode (solution rétrospective)
PR first time
Unity get component
Unity enables mobile phone vibration
cocos2dx_ Lua particle system
Sixth note
room数据库的使用
[paper notes] multi goal reinforcement learning: challenging robotics environments and request for research
[轉]: OSGI規範 深入淺出
[depth first search] 695 Maximum area of the island
Unity connects to the database
Unity3d learning notes
服务熔断 Hystrix
Development error notes
Under the national teacher qualification certificate in the first half of 2022
Data is stored in the form of table