Thursday, January 5, 2017

UVa 10018: Reverse and Add

Problem Link:Reverse and Add

NB:There is trick to solve this problem thus read the problem carefully.You may get compile error in C for input format.So implement the program in .cpp extension.

Source code in C++:

#include<stdio.h>
#define lli long long int
#define file freopen("in.txt","rt",stdin)
int main()
{
    // file;
    lli num,revnum,sum1,sum2,d,r,cnt,num1;
    int test;
    while(scanf("%d",&test)!=EOF)
    {
        while(test--)
        {
            cnt=0;
            scanf("%lld",&num);
            do
            {
                num1 = num;
                sum1=0;
                while(num>0)
                {
                    r = num%10;
                    num = num/10;
                    sum1 = sum1*10+r;
                }
                num=num1+sum1;
                sum2=0;
                while(num>0)
                {
                    r = num%10;
                    num = num/10;
                    sum2 = sum2*10+r;
                }
                cnt++;
                num=sum1+num1;
            }
            while((num1+sum1)!=sum2);
            printf("%lld %lld\n",cnt,sum2);
        }
    }
    return 0;
}

 

No comments:

Post a Comment