ask(4,2)太大,无法正常输出
#include <iostream>
#include <stdlib.h>
#include <vector>
#include <math.h>
using namespace std;
class TempClass
{
public:
TempClass();
TempClass(double m ,double n , double iResult);
~TempClass(){}
double m_M ;
double m_N ;
double m_iResult ;
};
TempClass::TempClass():m_M(0),m_N(0),m_iResult(0)
{
}
TempClass::TempClass(double m ,double n , double iResult):m_M(m),m_N(n),m_iResult(iResult)
{
}
class TempClass2
{
public:
TempClass2(){};
~TempClass2(){}
void AddResult(double m ,double n , double iResult);
bool IsHaveResult(double m ,double n, double* iResult = NULL );
private:
std::vector<TempClass> m_resultV ;
};
void TempClass2::AddResult( double m ,double n , double iResult )
{
if ( !IsHaveResult( m,n ) )
{
TempClass temp(m,n,iResult);
m_resultV.push_back(temp) ;
}
}
bool TempClass2::IsHaveResult(double m ,double n , double* iResult )
{
std::vector<TempClass>::iterator it = m_resultV.begin() ;
for ( ; it != m_resultV.end() ; ++it )
{
if ( it->m_M == m && it->m_N == n )
{
if ( NULL != iResult )
{
*iResult = it->m_iResult ;
}
return true ;
}
}
return false ;
}
TempClass2 temp ;
double
ack(double m,double n)
{
double iResult = 0 ;
if ( temp.IsHaveResult( m,n,&iResult))
{
return iResult ;
}
if(n==0)
{
iResult = (ack(m-1,1));
}else if(m==0)
{
iResult = 1 + n ;
}else if ( m == 1 )
{
iResult = 2 + n ;
}else if ( m == 2 )
{
iResult = 3 + 2*n ;
}else if ( m == 3 )
{
iResult = 5 + 8*(pow((double)2,n) -1);
}else
{
iResult = ack(m-1, ack(m, n-1));
}
temp.AddResult(m,n,iResult);
return iResult ;
}
int _tmain(int argc, char *argv[])
{
ack(1,2);
ack(1,3);
ack(2,3);
ack(3,2);
ack(4,2);
double iResult = 0 ;
temp.IsHaveResult( 1 , 2 , &iResult ) ;//ack(1,2)
cout<<"ack(1,2) = "<<iResult<<endl ;
temp.IsHaveResult( 1 , 3 , &iResult ) ;//ack(1,3)
cout<<"ack(1,3) = "<<iResult<<endl ;
temp.IsHaveResult( 2 , 3 , &iResult ) ;//ack(2,3)
cout<<"ack(2,3) = "<<iResult<<endl ;
temp.IsHaveResult( 3 , 2 , &iResult ) ;//ack(3,2)
cout<<"ack(3,2) = "<<iResult<<endl ;
temp.IsHaveResult( 4 , 2 , &iResult ) ;//ack(4,2)
cout<<"ack(4,2) = "<<iResult<<endl ;
_sleep(10000) ;
return 0;
}