C语言求三位数的回文数,回文数 C语言求解

#9

unsigned long getRevNum(unsigned long x, int n)

{

unsigned long y=0;

for(i=0;i

{ y*=10;

y+=x%10;

}

return y;

}

unsigned long sumrev(unsigned long x, int n)

return x+ getRevNum(x,n);

)

int isplalindrome(unsigned long x,int n)

{ int i,j=1;

int res=1;

for(i=1;i

if(x

for(i=10;i<=j;i*=10,j/=10)

{

if(x/j ==x%i){res=0;break;}

}

return res;

}

int is_8plalindrome(unsigned long x)

{

int n=2;

unsigned long t=x;

unsigned long v=100;

if(x<12)return -2;//比12小

if(x>100)return -1;//比100 大

if(x==100)return 1;//101 是回文,假设是

for(i=0;i<8;i++)

{

x=sumrev( x,n);

if(x>v){

n++;

v*=10;

}

if(isplalindrome(x,n))return i+1;

}

return 0;

}

int main()

{

unsigned long x ,t;

int sucess= scanf("%u",&x);

while {sucess >0 && (x<=100 && x>=12))

{

t=is_8plalindrome(x);

if(t>=0)

printf("%u ",t);

sucess = scanf("%u",&x);

}

return 0;

}

#9 unsigned long getRevNum(unsigned long x, int n) { unsigned long y=0; for(i=0;i { y*=10; y+=x%10; } return y; } unsigned long sumrev(unsigned long x, int n) return x+ getRevNum(x,n); ) int isplalindrome(unsigned long x,int n) { int i,j=1; int res=1; for(i=1;i if(x for(i=10;i<=j;i*=10,j/=10) { if(x/j ==x%i){res=0;break;} } return res; } int is_8plalindrome(unsigned long x) { int n=2; unsigned long t=x; unsigned long v=100; if(x<12)return -2;//比12小 if(x>100)return -1;//比100 大 if(x==100)return 1;//101 是回文,假设是 for(i=0;i<8;i++) { x=sumrev( x,n); if(x>v){ n++; v*=10; } if(isplalindrome(x,n))return i+1; } return 0; } int main() { unsigned long x ,t; int sucess= scanf("%u",&x); while {sucess >0 && (x<=100 && x>=12)) { t=is_8plalindrome(x); if(t>=0) printf("%u ",t); sucess = scanf("%u",&x); } return 0; }
经验分享 程序员 微信小程序 职场和发展