A plus B... again

Given two integers A and B (of no more than 100000 digits each), find their exact sum.

All Submissions
Best Solutions

Point Value: 15
Time Limit: 3.00s
Memory Limit: 16M
Added: Oct 24, 2008

Problem Types: [Show]

Languages Allowed:
C++03, PAS, C, ASM, C#, C++11

Comments (Search)

Are A and B on the same line separated by a space or two separate lines?

Separate lines.

This took a while to get. Can anyone help shorten my code and possibly how to make it so that my code doesn't have those extra if statements at the end? Thnx

I just wanted to make sure that the only thing that I need to add in my program is adding one negative and one positive numbers right?

Assuming you have correctly implemented the sum of two positive numbers and the sum of two negative numbers, logically, yes, all that remains is handling the cases in which one is positive and one is negative.

Make sure it's done correctly though tongue.gif

What is the correct answer for 6th and 8th test case?? I just want to compare it to what I am outputting....

You have the correct number of digits...they just seem to pretty much all be wrong >.<

Thanks, that helped me very much!! :):)

After changing the way i read in the file (numbers),i found that the judge accepted the output that my program outputted, but when tested on my computer, i found that the program outputs an extra zero for the sum.
Why is the judge accepting a wrong answer (or is the version of pascal that i am using just messed up)? Also, is this basically the same problem i had before?

I'm sorry, what cases are you talking about? (I cannot see the output of your program on the judge if it is judged as correct; only when there is a problem can I see it.)

I do notice that for several cases you printed '0' instead of a minus sign.

At my computer, the program outputs an extra zero for all sums of two positive integers (making it incorrect), though it is accepted here on the judge.

Are you sure it's 26?

I looked through your submission and it appears you had runtime error 216.
This indicates a general protection fault, probably caused by going out of bounds in memory.

I still seem to be getting an error. I think its because I am not reading the input properly.

Any cryptic clues?? :P

There are a lot of errors in your program - I think you need to learn a bit more about C++ before plugging in stuff from tutorials. (sizeof doesn't do what you think it does)

Why are you using char arrays? Just use strings.

ok I am now using strlen(variable); but it doesn't work on int arrays. I don't see how I can I get their lengths.

And I am even on the right track??

Hanson is correct; you should use strings. (This doesn't work in pascal because pascal strings are only 255 chars.)

If you use strings, just use s.length(), where s is a string.

But thats not the problem. I am reading in fine for sure but the thing is my solution requires to have a int array of a certain size and use that to get the answer but I don't know how to find the size of the array for the loop to go upto.

Take a look at my solution. I tried to fix the problem, it still isn't working. :(


say I have the following:

string str;

the length of this string is str.length().

so i can do this:

for (i=0;i<str.length();i++)

which would print the characters of the string.

Yes, I know. But, it doesn't seem work if I want to find the length of an int array.

KEep a running size count, which is easy to do, or use a vector, in which case you can indeed do:

int vector_size = v.size();

and that returns the number of elements in the vector.

however, vectors are a bit more advanced to use, so as Hanson suggested, become more proficient in C++.

Please, at least TRY to follow other people's advice BEFORE you post another complaint.

It was suggested twice that you use strings, and I've looked at your submissions and I still see char arrays.

I already wrote another program without ever using chars. Then I modified the first program and it seemed to get me 20/100 so I am using that now. Check my 7:05 submission.

How do I read and store the values into a char array? I can't seem to get a for loop working and ending when the user presses 'Enter'.

Can someone help me?

i: longint;
a: ansistring;
i := length(a);

#include <iostream>
#include <cstring>
using namespace std;

int i;
char a[1000];
int main()
cin >> a; //gets(a) for more speed
i = strlen(a);
return 0;

Why does my program output 1000 for the sum of -1 and 10000, but outputs 9999 at home?

Your code is extremely long, I don't think anyone wants to look through that and see exactly what's wrong.

Firstly, you mention that one case of 10000 and -1. But does your program work at home on those other cases, such as 10 and 56? If not, fix those first.

But in general, if your program behaves differently at home than on the judge, it means you're going out of bounds. So look over your code carefully - every time you access an array (like x[a]), make sure 1<=a<=(size of x).

Is initialization the only way to fix this (cuz I already did that)

I'm sure people have pointed this out to you before. But if you want people to debug your code, at least make it readable. Go back and look at other people's code for programs you already solved and see how they indent. If you learn how to indent properly, your code will be much easier to read for everyone else.

Does my program really output 6 for 10 and 56, and 1000 for -1 and 10000 - it doesn't happen in the school comp, my home comp, on my free pascal, or on TP7 (or is it outputting something else, but the judge shows back something different)

Just read my last post!

Yes, your program is outputting wrong answers.
No, initialization probably won't make a big difference, but you should ALWAYS do it.
As I said, you're probably going out of bounds.

As said below the sum can be up to 100,000 digits long.
I imagine extended only fits about 19 or so..

Why would the problem be worth 15 points if you could do it in about 10 lines?

It's weird how there's a big gap in the solution times.
It's either faster that 0.1 seconds, or slower than 1.5 seconds...

there at least 2 ways to do this problem, some people coded the fast way, and some people coded the slow way.

Ah yes, I see why now. The slow solutions involve padding the shorter string with zeroes on the front until it is as long as the longer string. When you add a character to the beginning of a string, all subsequent characters get pushed one byte to the right (like in insertion sort). If you do this multiple times, it incurs a huge cost in time.