Monday, October 1, 2018

UVa problem solution reverse and add -10018

Discuss: in this problem just simply reverse the digit using very naive process that is also fine. and then whether it is palindrome or not if it is not palindrome then repeat the process for newly create number. you will get your answer,
there is no tricky part  in this problem.

try yourself, before see the code



#include<bits/stdc++.h>
using namespace std;
bool palindrome(long long n){
    long long m=n,r=0,a;
   while(m){
    r*=10;
    r+=m%10;
    m/=10;
   }
   if(r==n)
    return true;
   return false;

}
int main(){
    //freopen("input.txt","r",stdin);
    //freopen("out.txt","w",stdout);
int t,j;
cin >>t;
for(j=1; j<=t; j++)
{
    long long n,i;
    cin >>n;
 long long m=n,r=0,a,cnt=0;
while(1){


   cnt++;
   while(m){
    r*=10;
    r+=m%10;
    m/=10;
   }
    a=n+r;
    if(palindrome(a)){
       break;
    }
      else{
        n=a;
        m=a;
        r=0;
      }
}
cout <<cnt<<' '<<a<<endl;
}
return 0;

}

No comments:

Post a Comment