这是我尝试过的,但它给了我错误的输出.任何人都可以指出错误是什么?
function superPower($n) {
$response = false;
$n = abs($n);
if ($n < 2) {
$response = true;
}
for ($i=2;$i<$n;$i++) {
for ($j=2;$j<$n;$j++) {
if (pow($i,$j) == $n) {
$response = true;
}
}
}
return $response;
}
Run Code Online (Sandbox Code Playgroud)
例如,如果我给它编号25,它给出1作为输出.//正确但如果我给它26它仍然给我1错了.
通过使用superPower,你实际上是在尝试对攻击的威力进行一定的防御,看看它是否能够保持稳定.与现在的蛮力方法相比,这可以更有效地完成.
function superPower( $hp) { // Niet used Superpower!
if( $hp <= 1) return true;
for( $def = floor(sqrt($hp)); $def > 1; $def--) { // Niet's Defence fell
for( $atk = ceil(log($hp)/log($def)); $atk > 1; $atk--) { // Niet's Attack fell
if( pow($def,$atk) == $hp) return true;
break;
// you don't need the $atk loop, but I wanted to make a Pokémon joke. Sorry.
}
// in fact, all you really need here is:
// $atk = log($hp)/log($def);
// if( $atk-floor($atk) == 0) return true;
}
return false;
}
Run Code Online (Sandbox Code Playgroud)