Jan 21, 2013
In this post I will show you my approach to adding reversed numbers. This problem occurred during the ACM Central European Programming Contest, in Prague in 1998.
General description of the problem #
The input consists of N cases (equal to about 10000). The first line of the input contains only positive integer N. Then follow the cases. Each case consists of exactly one line with two positive integers separated by space. These are the reversed numbers you are to add.
For each case, print exactly one line containing only one integer - the reversed sum of two reversed numbers. Omit any leading zeros in the output.
3 24 1 4358 754 305 794
34 1998 1
Exemplary approach #
- In the first line of the input, we put number of the lines to process.
- In the next lines, we put numbers split by space, in which we should reverse order of the digits and then add them.
- In the final step, we should reverse order of the digits in the generated data and write result to the output.
Proposal of the solution in the Ruby language #
def addrev(n) b = 0 n.split(' ').each do |a| b+= a.to_s.reverse.to_i end return b.to_s.reverse.to_i end a = true i = j = 0 out =  $stdin.each_line do |line| if(!a) then if(j != i) out.push(addrev(line.to_s)) j += 1 else break end else i = line.to_i a = false end end out.each do |b| $stdout << b << "\n" end