Solution of 713 - Adding Reversed Numbers

Problem Description
source: https://uva.onlinejudge.org/external/7/713.html

The Antique Comedians of Malidinesia prefer comedies to tragedies. Unfortunately, most of the ancient plays are tragedies. Therefore the dramatic advisor of ACM has decided to transfigure some tragedies into comedies. Obviously, this work is very hard because the basic sense of the play must be kept intact, although all the things change to their opposites. For example the numbers: if any number appears in the tragedy, it must be converted to its reversed form before being accepted into the comedy play.

 Reversed number is a number written in arabic numerals but the order of digits is reversed. The first digit becomes last and vice versa. For example, if the main hero had 1245 strawberries in the tragedy, he has 5421 of them now. Note that all the leading zeros are omitted. That means if the number ends with a zero, the zero is lost by reversing (e.g. 1200 gives 21). Also note that the reversed number never has any trailing zeros. 


ACM needs to calculate with reversed numbers. Your task is to add two reversed numbers and output their reversed sum. Of course, the result is not unique because any particular number is a reversed form of several numbers (e.g. 21 could be 12, 120 or 1200 before reversing). Thus we must assume that no zeros were lost by reversing (e.g. assume that the original number was 12). 

Input 

The input consists of N cases. 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. Numbers will be at most 200 characters long. 

Output 

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. 

Sample Input 


24 1 
4358 754 
305 794 

Sample Output

 34 
1998 
1

Solution
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <fstream>
#include <iostream>
#include <string>
#include<stdio.h>
#include<stdlib.h>

using namespace std;
void Addition(int length);
 char str1[250],str2[250],result[250];
 int len3;
 int main()
 {
     int i,j,len1,len2,t,k,difr;
     //freopen("in.txt","r",stdin);
     while(scanf("%d",&t)==1)
     {
         for(k=1;k<=t;k++)
         {
             scanf("%s %s",&str1,&str2);
             len3=0;
             len1=strlen(str1);
             len2=strlen(str2);
             if(len1>len2)
             {
                 difr=(len1-len2)+len2;
                 for(i=len2;i<difr;i++)
                    str2[i]='0';
                Addition(len1);
             }
             else
             {
                 difr=(len2-len1)+len1;
                 for(i=len1;i<difr;i++)
                    str1[i]='0';
                Addition(len2);
             }
             i=1;
             while(result[i]=='0')
             {
                 i++;
             }
             for(j=i;j<=len3;j++)
                printf("%c",result[j]);
             printf("\n");
             memset(str1,'\0',sizeof(str1));
             memset(str2,'\0',sizeof(str2));
             memset(result,'\0',sizeof(result));
         }
     }
     return 0;
 }
void Addition(int length)
{
    int i,tem,carry=0;
    len3=0;
    for(i=0;i<length;i++)
    {
     tem=(str1[i]-48)+(str2[i]-48)+carry;
     len3+=1;
     result[len3]=((tem%10)+48);
     carry=tem/10;
    }
    if(carry>0)
    {
        len3+=1;
        result[len3]=carry+48;
    }
}
image

No comments:

Post a Comment

Write your comment - Share Knowledge and Experience