acm The Least Palindromic NumberDescriptionPalindromic numbers are digital strings that read the same both forwards and backwards. For example, 121, 44 and 3 are Palindromic numbers, 175, 36 are not;For a given integer N, you are to find a palindromi

来源:学生作业帮助网 编辑:作业帮 时间:2024/05/13 07:21:09

acm The Least Palindromic NumberDescriptionPalindromic numbers are digital strings that read the same both forwards and backwards. For example, 121, 44 and 3 are Palindromic numbers, 175, 36 are not;For a given integer N, you are to find a palindromi
acm The Least Palindromic Number
Description
Palindromic numbers are digital strings that read the same both forwards and backwards. For example, 121, 44 and 3 are Palindromic numbers, 175, 36 are not;
For a given integer N, you are to find a palindromic number P that satisfy P>N. However, there are many such palindromic numbers. Your task is to find the least one.

Input
There are several test cases, each test case contains only one positive integer N in one line. The number of digits of N is not exceeding 10,000, and N has not lead zeroes.
The input will finish with the end of file.

Output
For each the case, your program will output the least palindromic number P (P > N) on separate line.
Sample Input
44
3
175
Sample Output
55
4
181
求高手解答.

acm The Least Palindromic NumberDescriptionPalindromic numbers are digital strings that read the same both forwards and backwards. For example, 121, 44 and 3 are Palindromic numbers, 175, 36 are not;For a given integer N, you are to find a palindromi
#include"stdio.h"
#include"string.h"
#include
using namespace std;
const int MAX=10005;
char s[MAX];
struct BigNum
{
int dig[MAX];
int len;
void Clr()
{
memset(dig,0,sizeof(dig));
len=1;
}
void print()
{
int i;
for(i=len-1;i>=0;i--)printf("%d",dig[i]);
}
};
BigNum GetN(char s[])
{
BigNum ret;

int i,j=0;
ret.Clr();
ret.len=strlen(s);
for(i=ret.len-1;i>=0;i--,j++)
ret.dig[j]=s[i]-'0';
return ret;
}
int cmp(BigNum a,BigNum b)
{
int i;
for(i=a.len-1;i>=0;i--)if(a.dig[i]!=b.dig[i])return a.dig[i]-b.dig[i];
return 0;
}
void GetHalf(BigNum ret)
{
int s,i,n=ret.len;
int len=ret.len,j;
BigNum x;
x.Clr();
if(ret.len%2==0)
{
x=ret;
s=n/2;
i=s;
for(j=i-1;j>=0;j--,i++)x.dig[j]=ret.dig[i];
if(cmp(x,ret)>0)
{
x.print();
return;
}
i=s;
ret.dig[s]++;
while(ret.dig[i]>9)
{

ret.dig[i+1]++;
ret.dig[i]-=10;
i++;
}
if(ret.dig[ret.len]>0)
{
ret.len++;
printf("1");
i=ret.len-2;
while(i>0)
{
putchar('0');
i--;
}
printf("1");
}
else
{
for(i=len-1;i>=s;i--)printf("%d",ret.dig[i]);
for(i=s;i=0;j--,i++)x.dig[j]=ret.dig[i];
if(cmp(x,ret)>0)
{
x.print();
return;
}
i=s;
ret.dig[s]++;
while(ret.dig[i]>9)
{

ret.dig[i+1]++;
ret.dig[i]-=10;
i++;
}
if(ret.dig[ret.len]>0)
{
ret.len++;
printf("1");
i=ret.len-2;
while(i>0)
{
putchar('0');
i--;
}
printf("1");
}
else
{

for(i=len-1;i>=s;i--)printf("%d",ret.dig[i]);
for(i=s+1;i