You’re given two numbers. Sum them up without carrying.
QUICK EXPLANATION:
Just do what is written in statement.
EXPLANATION:
One of possible codes to solve the problem:
int a, b;
cin >> a >> b;
vector<int> A, B;
while(a) {
A.push_back(a % 10);
a /= 10;
}
while(b) {
B.push_back(b % 10);
b /= 10;
}
while(A.size() < B.size()) A.push_back(0);
while(B.size() < A.size()) B.push_back(0);
for(int i = 0; i < A.size(); i++) {
A[i] += B[i];
}
int ans = 0;
reverse(begin(A), end(A));
for(auto it: A) {
ans = ans * 10 + it % 10;
}
cout << ans << endl;
We can just find the actual sum and keep subtracting 10,100,1000… from it by looping the original numbers till the number with less number of digits gets exhausted(we subtract only if we get a carry from the previous place value).
There is a typo below PROBLEM. It should be “Sum them up without carrying.”
I wanted to comment and not post an answer, but I couldn’t find a comment button. I am new here and couldn’t locate the comment button. Please help.
EDIT 1:
Nevermind, I found it below my answer under more option. I think we have to first post an answer and then we can convert it to comment below question or someone’s answer. And yeah BTW the links for author’s solution and Tester’s solution aren’t working.
If you really can convert your answer to a comment that’s a bug, not a feature
About the solution links, it’s not under my control but I am letting the admins know.
Its a known issue. Dont worry. BTW, you shouldnt actually do that if you want to be away for trouble. Word is, that one of the mods is reallllllyyyy strict.
@vijju123 I wasn’t exploiting the bug. I just came to know about it and I tested it only once for surety. I reported it immediately when I was sure about it. I am sorry if I offended someone or made someone angry, but I meant no harm.
#include<bits/stdc++.h> #define ll long long
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin>>t;
while(t–)
{
string s,s1;
cin>>s>>s1;
int x=s.length()-1,y=s1.length()-1;
vector v;
while(x>=0&&y>=0)
{
int tem=s[x]-‘0’;
int tem1=s1[y]-‘0’;
tem+=tem1;
if(tem>9)
v.push_back(tem%10);
else
v.push_back(tem);