BUGCAL - Editorial

PROBLEM LINK:

Practice

Contest

Author: Trung Nguyen

Tester: Oleksandr Kulkov

Editorialist: Oleksandr Kulkov

DIFFICULTY:

CAKEWALK

PREREQUISITES:

None

PROBLEM:

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;

AUTHOR’S AND TESTER’S SOLUTIONS:

Author’s solution can be found here.

Tester’s solution can be found here.

RELATED PROBLEMS:

2 Likes

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).

Here is the link to my solution:- My Solution

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.

Here is my approach to this problem ::

https://www.codechef.com/viewsolution/17941473

Could not pass the subtask 2:

#include <iostream>
#include<string>
#define ll long long int
using namespace std;

int main()
{
 ll t;
 cin>>t;
 while(t--){
    string a, b;
    cin>>a>>b;
    ll max_last = max(a.size(), b.size());
    ll min_last = min(a.size(), b.size());
    ll result[max_last];
    ll i=max_last-1;
    for(ll  j= min_last-1; j>=0; i--, j--){
        ll sum = a[i]-'0' + b[j]-'0';
        result[i] = sum ;
    }
    while(i>=0){
        result[i] = a[i]-'0';
        i--;
    }

    ll sum = 0;
    for(ll i=0; i<max_last; i++)
    {
        sum = sum*10 + result[i]%10;
    }
    cout<<sum<<"\n";
  }
  return 0;
}

Love the quick explanation :stuck_out_tongue:

2 Likes

There is a comment button but it will be available when you have at least 50 karma. Also thanks for pointing out, I’ll correct the typo :slight_smile:

@meooow But I can still make my answer as comment. Then what difference will it make when I get 50 points?

And what about the solution links not working?

If you really can convert your answer to a comment that’s a bug, not a feature :stuck_out_tongue:
About the solution links, it’s not under my control but I am letting the admins know.

Let me check by posting an answer if that option really works.

It really works…!!

Haha, well good then. Also the solution links are working now.

Thank you. And BTW Should I report this bug to codechef? Are you sure users with less than 50 Karma points can’t comment?

@meooow I have mailed to bugs@codechef.com about this. Lets see what they have to say about it and What is the outcome.

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.

In your first loop, you can’t be sure that i applies to a[] andj applies to b[].

#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);

        x--;
        y--;
    }
    while(x>=0)
    {
        v.push_back(s[x]-'0');
        x--;
    }
    while(y>=0)
    {
        v.push_back(s1[y]-'0');
        y--;
    }
    for(int i=v.size()-1;i>=0;i--)
    cout<<v[i];
    cout<<endl;
    
    
}

}
can anyone please help me 2nd task is not passed … please check once

vector v; it is declared correctly :grin: but not printed there please once check any mistake is there

please see what’s wrong with my code for subtask 2.

https://www.codechef.com/viewsolution/30034312