spoj The Next Palindrome

spoj The Next Palindrome
这个题真的好烦。。

别的就不写了。。贴几个数据

7
999
898
1
45221
452511
43349
808

只记得这么几个了。。。这几个都出过错。。

以下是代码。。。
坚持一点oj没错。。如果一直是wa。。那么出错的肯定是你!

#include < iostream >
#define MaxK 
2000005
char k[MaxK];
int  down[MaxK];
void solve()
{
    
int   len = ( int )strlen(k),i,j,c;
    bool tag
= true ;
    
for (i = 0 ;i < len ;i ++ )
        
if (k[i]! = ' 9')
            break;
    
if (i >= len )
    {
        
for (i = 1 ;i < len ;i ++ )
            k[i]
= ' 0';
        k[ 0 ] = k[ len ] = ' 1';
        k[ len + 1 ] = ' \0';
        return;
    }
    
if ( len % 2 )
        i
= len / 2 - 1 ,j = i + 2 ;
    
else
        i
= ( len - 1 ) / 2 ,j = i + 1 ;
    
while (i >= 0 && j < len )
    {
        
if (k[i] > k[j])
            down[j]
=- 1 ; // k[j]up
        
else   if (k[i] < k[j])
            down[j]
= 1 ; // k[j]down
        i
-- ,j ++ ;
    }
    
if ( len % 2 )
        i
= len / 2 + 1 ;
    
else  i = len / 2 ;
    
for (;i < len ;i ++ )
    {
        
if (down[i] ==- 1 )
            break;
        
else   if (down[i] == 1 )
        {
            tag
= false ;
            break;
        }
    }
    
if (i >= len )tag = false ;
    
for ( len % 2 ?i = len / 2 + 1 :i = len / 2 ;i < len ;i ++ )

        
if (down[i])
        {
            k[i]
= k[ len - 1 - i];
    }
    
if (!tag)
    {
            i
= len / 2 ,c = 1 ;
            
while (i >= 0 && c)
            {
                c
= (k[i] - ' 0'+1)/10;
                k[i] = ((k[i] - ' 0')+1)%10+'0';
                k[ len - 1 - i] = k[i];
                i
-- ;
            }
    }
}
    
int  main()
{
    
int  T;
    scanf(
" %d " , & T);
    
while (T -- )
    {
        memset(down,
0 ,sizeof(down));
        memset(k,
0 ,sizeof(k));
        scanf(
" %s " ,k);
        solve();
        printf(
" %s\n " ,k);
    }
    return 
0 ;
}

你可能感兴趣的:(spoj The Next Palindrome)