2018年1月13日 星期六

UVA Q10018 - Reverse and Add

#include<iostream>
#include<iomanip>
#include<cstring>
#include<math.h>
#include<algorithm>
using namespace std;
int countd(int a){
    int d=0;
    for(int x=a;x>0;x=x/10){
        d++;
    }
    return d;
}
int checkrev(int a){
    int d=0,e,f,g;
    d=countd(a);
    for(int x=1;x<=d/2;x++){
        e=pow(10,x);
        g=pow(10,x-1);
        f=(a/pow(10,(d-x)));
        if((a%e)/g!=(f%10)){
            return 0;
        }
    }
    return 1;
}
int main(){
long long n,a,t,b,c;
cin>>n;
for(int x=0;x<n;x++){
    cin>>a;
    t=0;
    while(1){
        t++;
        b=a;
        c=0;
        while(b>0){
            c=(c*10)+(b%10);
            b=b/10;
        }
        a=a+c;
        if(checkrev(a)){
            break;
        }
    }
    cout<<t<<" "<<a<<endl;
}


system("pause");
return 0;
}

沒有留言:

張貼留言