CodeChef : Problem 1 (CMB01)


Title : Problem 1

Link : http://www.codechef.com/problems/CMB01/

Problem code: CMB01

Tricky Lines :

  1. if the number ends with a zero, the zero is lost by reversing
  2. reversed number never has any trailing zeros
  3. we must assume that no zeros were lost by reversing
  4. Omit any leading zeros in the output

Analysis :

  1. reverse the two input number
  2. add them
  3. reverse the result
  4. omit preceding zeros if any

Runtime : 0.00s

Critical Input : 
5
50 50
120 210
1100 1200
2000 3000
2222 3210

Critical Output : 
1
33
23
5
5432

Solution :

// @BEGIN_OF_SOURCE_CODE

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cctype>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <sstream>
#include <cmath>
#include <bitset>
#include <utility>
#include <set>
#define INT_MAX 2147483647
#define INT_MIN -2147483648
#define pi acos(-1.0)
#define N 1000000
#define LL long long
using namespace std;

int main ()
{
    int testCase;
    scanf ("%d", &testCase);

    while ( testCase-- ) {
        char fir [20], sec [20], res [50];
        scanf ("%s %s", fir, sec);

        reverse (fir, fir + strlen (fir));
        reverse (sec, sec + strlen (sec));

        sprintf (res, "%d", atoi (fir) + atoi (sec));

        reverse (res, res + strlen (res));

        printf ("%d\n", atoi (res));
    }

	return 0;
}

// @END_OF_SOURCE_CODE

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s