//use recruve ,it's easy
int akm_recu( int m, int n )
{
if( 0 == m )
return n + 1;
else if( 0 == n )
return akm_recu( m - 1, 1 );
else
return akm_recu( m - 1, akm_recu( m, n - 1 ) );
};
// not recurve aalgorithms for ackerman funcition
// use three stack .
// when calc acm( 3,10 ) it take a long time
#define EXISTPARAM 1
#define NOTEXISTPARAM 0
int akm_nrecu( int m, int n )
{
int ret = 0;
int ParamM = 0;
int ParamN = 0;
int ParamFlag = 0;
StackPQ stack_m;
StackPQ stack_n;
StackPQ stack_flag;
stack_m.Push( m );
stack_n.Push( n );
stack_flag.Push( EXISTPARAM );
while( !stack_m.IsEmpty( ) && !stack_n.IsEmpty( ) ) {
ParamM = stack_m.Pop( );
ParamN = stack_n.Pop( );
ParamFlag = stack_flag.Pop( );
if( EXISTPARAM == ParamFlag ) {
if( 0 == ParamM )
ret = ParamN + 1;
else if( 0 == ParamN ) {
stack_m.Push( ParamM - 1 );
stack_n.Push( 1 );
stack_flag.Push( EXISTPARAM );
}
else {
stack_m.Push( ParamM - 1 );
stack_n.Push( 0 );
stack_flag.Push( NOTEXISTPARAM );
stack_m.Push( ParamM );
stack_n.Push( ParamN - 1 );
stack_flag.Push( EXISTPARAM );
}
}
else {
stack_m.Push( ParamM );
stack_n.Push( ret );
stack_flag.Push( EXISTPARAM );
}
}
return ret;
}
阅读(862) | 评论(0) | 转发(0) |