二分法,求问哪错了
http://acm.hdu.#include <stdio.h>
#include <string.h>
int main()
{
int abc( int *z, int l, int r, int n );
int z1[100000], z2[100000];
memset( z1, 0, sizeof(z1) );
memset( z2, 0, sizeof(z2) );
int i, j, k, l, m, n, max, a, b;
for( i = j = l = k = m = 1; i <= 151200 || j <= 151200; m++, i = m * m * m, j = m * (m+1) * (m+2) / 6.0 )
{
if( i <= 151200 )
z1[l++] = i;
if( j <= 151200 )
z2[k++] = j;
}
while( scanf( "%d", &n) != EOF )
{
i = 0;
a = 0;
max = 0;
b = 0;
if( n == 0 )
break;
a = abc( z1, 0, l-1, n );
b = abc( z2, 0, k-1, n );
if( a > b )
max = a;
else
max = b;
while( z1[i] <= a )
{
b = abc( z2 , 0, k-1, n-z1[i] ) + z1[i];
if( b > max )
max = b;
i++;
}
printf( "%d\n", max );
}
return 0;
}
int abc( int *z, int l, int r, int n )
{
int mid;
while( r - l > 1 )
{
mid = ( l + r ) >> 1;
if( z[mid] > n )
r = mid;
else if( z[mid] == n )
return n;
else
l = mid;
}
return z[r] > n ? z[l] : z[r];
}