我正试图用蛮力解决最新的Euler项目问题(我知道这不是最好的解决方案,但在我找出错误的地方之前,我只会使用它)。
我不明白,我到底做错什么了。我很确定我生成了正确的数字,但结果仍然是错误的:
function number_split( $nb )
{
GLOBAL $arr;
$length = strlen( $nb );
if( $length == 1 )
return true;
$count = 0;
for( $i=1; $i<$length; $i++ ) {
for( $j=0; $j<=$length-$i; $j++ ) {
$temp = substr( $nb, $j, $i );
if( $temp % $length == 0 ) {
$count++;
if( $count > 1 )
return false;
}
}
}
return ( $count == 1 );
}
$lim = 3;
$res = 0;
$start = gmp_strval( gmp_pow( 10, $lim-1 ) );
$end = gmp_strval( gmp_pow( 10, $lim ) );
for( $i=$start; $i<$end; $i++ ) {
$res += number_split( $i );
}
echo $res;我得到了378个应该有389的地方。我在这里做错什么了?
我不想要答案,只是想知道我的逻辑哪里错了。
问题是:

发布于 2013-02-06 14:02:27
你只看3位数字,但你需要看10^3以下的所有数字,包括1位数和2位数。换句话说,$start应该是1。
此外,您没有查看子字符串,它是整个原始字符串,因为$i (子字符串的长度)只运行到$length - 1。
https://stackoverflow.com/questions/14730425
复制相似问题