Skip to content

Instantly share code, notes, and snippets.

@pureexe
Created October 22, 2012 16:23
Show Gist options
  • Select an option

  • Save pureexe/3932355 to your computer and use it in GitHub Desktop.

Select an option

Save pureexe/3932355 to your computer and use it in GitHub Desktop.
ข้อ7 สอวนค่าย 1 หา Palindome ที่ใกล้ที่สุด
#include<stdio.h>
int chk(int in)
{
int u[100],ct=0,i;
if(in<0)
return 0;
while(in>=1)
{
u[ct++]=in%10;
in/=10;
}
for(i=0; i<ct; i++)
{
if(u[i]!=u[ct-i-1])
return 0;
}
return 1;
}
int findnear(int in)
{
int ct_r=in,ct_l=in;
if(chk(in))
{
printf("Yes");
return 0;
}
while(1)
{
if(chk(ct_r))
break;
ct_r++;
}
while(1)
{
if(chk(ct_l))
break;
ct_l--;
}
if(in-ct_l<ct_r-in)
printf("%d",ct_l);
else if(in-ct_l>ct_r-in)
printf("%d",ct_r);
else
printf("%d %d",ct_l,ct_r);
}
int main()
{
int in;
scanf("%d",&in);
findnear(in);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment