You are not logged in. Please login at www.codechef.com to post your questions!

×

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:

asked 24 Oct '17, 18:39

melfice's gravatar image

4★melfice
811937
accept rate: 0%

edited 30 Oct '17, 19:36

meooow's gravatar image

6★meooow ♦
7.3k720

1

Love the quick explanation :P

(30 Oct '17, 14:06) meooow ♦6★

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

(30 Oct '17, 20:03) vatsalsura2★

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

link

answered 30 Oct '17, 14:22

ramini's gravatar image

2★ramini
615
accept rate: 8%

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.

link

answered 30 Oct '17, 19:05

vatsalsura's gravatar image

2★vatsalsura
1659
accept rate: 8%

edited 30 Oct '17, 19:08

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

(30 Oct '17, 19:36) meooow ♦6★

@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?

(30 Oct '17, 19:57) vatsalsura2★

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

(30 Oct '17, 20:01) meooow ♦6★

It really works...!!

(30 Oct '17, 20:03) vatsalsura2★

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

(30 Oct '17, 20:07) meooow ♦6★

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

(30 Oct '17, 20:10) vatsalsura2★

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

(30 Oct '17, 22:02) vatsalsura2★

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.

(30 Oct '17, 22:48) vijju123 ♦♦5★

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

(30 Oct '17, 23:48) vatsalsura2★
showing 5 of 9 show all

Here is my approach to this problem ::

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

link

answered 24 Mar '18, 03:45

harrypotter0's gravatar image

4★harrypotter0
318110
accept rate: 1%

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;
}
link

answered 22 Sep '18, 01:11

ezazul_haque's gravatar image

1★ezazul_haque
1
accept rate: 0%

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

(22 Sep '18, 05:47) joffan5★
toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×15,852
×1,688
×72
×64
×10

question asked: 24 Oct '17, 18:39

question was seen: 2,201 times

last updated: 22 Sep '18, 05:47