),所以判斷19u是否是重復數(通過簡單地判斷19u的個位和十位是否相同。)。
),所以問題變成了求2010u的不重復數()。
)。
#include <stdio.h>
main(
( i = ; i < + ; i++ printf ( , i , find( i +
printf( , n ) ;
( n <
( n / < ( t = find( n / n = t *
( n % == n / % find( n +
}
#include <stdio.h>
typedef struct
{
unsigned char t[ 20 ] ; //這個空間應該夠了
int top ; //記錄第一位的下標
}
Map ;
unsigned find( unsigned );
void parse( Map * , unsigned ) ;
int search ( const Map * );
void add_1( Map * , const int );
void clear( Map * , const int , const int );
unsigned combi( const Map * );
int main( void )
{
unsigned iu ;
//測試
for ( iu = 19922884u ; iu < 19922884u + 1u ; iu++ )
{
printf ( "%u %u\n" , iu , find( iu + 1u ) );
}
return 0;
}
unsigned combi( const Map * p_m )
{
unsigned nu = 0u ;
int i;
for ( i = p_m->top ; i >= 0 ; i -- )
{
nu *= 10u ;
nu += p_m->t[i] ;
}
return nu;
}
void clear( Map * p_m , const int from , const int to )
{
int i ;
for ( i = from - 1 ; i > to - 1; i -- )
p_m->t[i] = 0u ;
}
void add_1( Map * p_m , const int from )
{
int i ;
p_m->t[from] ++; //最低位加1
for ( i = from ; i < p_m->top ; i ++ ) //進位處理
{
p_m->t[i + 1] += p_m->t[i] / 10u ;
p_m->t[i] %= 10u ;
}
if ( p_m->t[p_m->top] > 9u ) //最高位有進位
{
p_m->t[p_m->top + 1] = p_m->t[p_m->top] / 10u ;
p_m->t[p_m->top ++ ] %= 10u ;
}
}
int search ( const Map * p_m )
{
int i ;
for ( i = p_m->top ; i > 0 ; i-- )
{
if ( p_m->t[i] == p_m->t[i-1] )
break ;
}
return i - 1 ;
}
void parse( Map * p_m , unsigned n )
{
p_m->top = -1 ;
while ( n > 0u )
{
p_m->t[ ++ p_m->top ] = n % 10u ;
n /= 10u ;
}
}
unsigned find( unsigned n )
{
Map map ;
int end = 0 , b_point ;
parse( &map , n ) ; //將n分解為數字
while ( ( b_point = search ( &map ) ) > -1 )//為-1時說明不是重復數
{
add_1( &map , b_point ); //重復數部分加1
clear( &map , b_point , end ); //後面改為0
end = b_point ; //確定下次循環的處理范圍
}
return combi( &map );
}
0 0 2