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)


It's not like it makes it any easier, we still have to use string manipulation because java variables can't store such big integers.

Java has a built-in BigInteger class. A sample Java solution for this problem is as follows:

import java.util.Scanner; 
import java.math.BigInteger;

public class aplusb2 {
public static void main(String[] foo) {
Scanner in = new Scanner(System.in);
System.out.println((new BigInteger(in.next())).add(new BigInteger(in.next())));

What's the difference between
public static void main(String[] args)
public static void main(String[] foo)?

yes, otherwise the problem would be trivial.

The program is not working!
Is there a catch? I have used the same program as the
first A + B.
For Some reason it no work!
Its very Simple!


readln (a);
readln (b);
writeln (a+b);

nothing to see here whistle.gif


Integers only have a capacity of -231 to 231 -1 which is 10 digits long while this problem has input that may be up to 100 000 digits each. So you'll have to find another method to solve this. For future reference don't post your code in the comments and please refer to this before posting a question.


The same as the title

It's exactly as the problem states: add two numbers together, each of which may be up to 100 000 digits long.

Note that python is disallowed because it has bignums.

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.