红色那段代码改改后C语言也能用
#include <algorithm>
bool foo( const unsigned steps[], size_t size )
{
for( size_t a=0,b=1; a!=b; ) // [a,b)
{
if( b >= size )
return true;
size_t c = b-1;
for( size_t i=a; i!=b; ++i )
c = std::max( c, i+steps[i] );
a = b;
b = c+1;
}
return false;
}
#include <iostream>
#include <vector>
void test( const std::vector<unsigned>& steps, bool result )
{
bool r = foo( steps.data(), steps.size() );
if( r != result )
{
std::cerr << "foo( {" << steps[0];
for( size_t i=1; i!=steps.size(); ++i )
std::cerr << ',' << steps[i];
std::cerr << " } should be " << (result?"true.\n":"false.\n");
}
}
int main( void )
{
//test( {2,3,1,1,4}, true );
//test( {3,2,1,0,4}, false );
//test( {3,3,1,0,4}, true );
test( {3,0,0,0,4}, false );
}