ural 1123

找大于等于原数的最小回文数字  代码比较烂...........

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std;
char s[2010];
int main()
{
scanf("%s",s);
int len = strlen(s);
int l = 0, r = len - 1;
int flag = 0;
int T = 0;
while(r>=l)
{
if(r - l > 1)
{
if(s[l] < s[r])
flag = 1;
s[r] = s[l];
}
else if(r - l == 1)
{
if(s[r] > s[l])
s[l] = s[r];
else if(s[l] > s[r])
{
s[r] = s[l];
}
else
{
if(flag)
{
if(s[r] != '9')
{
s[r] += 1;
s[l] += 1;
}
else
while(1)
{
if(s[r] == '9')
{
s[r] = '0';
s[l] = '0';
r++;
l--;
}
else
{
s[r] += 1;
s[l] += 1;
T = 1;
break;
}
}
}
}
}
else
{
if(flag)
{
if(s[r] != '9')
s[r] += 1;
else
while(1)
{
if(s[r] == '9')
{
s[r] = '0';
s[l] = '0';
r--;
l++;
}
else
{
s[r] += 1;
s[l] += 1;
break;
}
}
}
}
if(T)
break;
l++;
r--;
}
puts(s);
return 0;
}
上一篇:学IT技术 轻松高薪就业


下一篇:JavaScript Comparison and Logical Operators