如何在php中实现FibonacciSecret
我试图获取该消息中所有字符在斐波那契序列中的位置,斐波那契序列由斐波那契数按升序排序而成。请忽略空白字符并使用扩展斐波那契 返回获得的字符,这些字符大写并由“-”字符连接 范例 《达芬奇密码》是丹·布朗2003年的一部神秘侦探小说 输出应该是 FibonacciSecretmessage=T-H-H-E-D-V-C-E-M-T 第一个斐波那契是0,那么第一个字母是T 第二个斐波那契是1,那么第二个字母是H 第三个斐波那契是1,那么第三个字母是H。。。等等 因此,答案应该是T-H-H-E-D-V-C-E-M-T 试码 预期产量 答案应该是T-H-H-E-D-V-C-E-M-T如何在php中实现FibonacciSecret,php,Php,我试图获取该消息中所有字符在斐波那契序列中的位置,斐波那契序列由斐波那契数按升序排序而成。请忽略空白字符并使用扩展斐波那契 返回获得的字符,这些字符大写并由“-”字符连接 范例 《达芬奇密码》是丹·布朗2003年的一部神秘侦探小说 输出应该是 FibonacciSecretmessage=T-H-H-E-D-V-C-E-M-T 第一个斐波那契是0,那么第一个字母是T 第二个斐波那契是1,那么第二个字母是H 第三个斐波那契是1,那么第三个字母是H。。。等等 因此,答案应该是T-H-H-E-D-V-
谁能告诉我。这一次我哪里出错了?我想你的斐波那契逻辑错了。我在这里找到了一个例子: 顺序: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34;
<?php
$message = 'The Da Vinci Code is a 2003 mystery-detective novel by Dan Brown';
$str_split = str_split(str_replace(' ', '', $message));
$n_value = 10;
$first_value = 0;
$second_value = 1;
$next_value = 0;
$c_value = 0;
$letters = [];
for ( $c_value = 0 ; $c_value < $n_value ; $c_value++ )
{
if ( $c_value <= 1 ) {
$next_value = $c_value;
} else {
$next_value = $first_value + $second_value;
$first_value = $second_value;
$second_value = $next_value;
}
$letters[] = $str_split[$next_value];
}
echo implode($letters, "-");
输出:
T-h-h-e-D-V-c-e-m-T
在此处运行它:
下面是一个有趣的PHP斐波那契参考线程:
我认为你的斐波那契逻辑是错误的。我在这里找到了一个例子: 顺序: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34;
<?php
$message = 'The Da Vinci Code is a 2003 mystery-detective novel by Dan Brown';
$str_split = str_split(str_replace(' ', '', $message));
$n_value = 10;
$first_value = 0;
$second_value = 1;
$next_value = 0;
$c_value = 0;
$letters = [];
for ( $c_value = 0 ; $c_value < $n_value ; $c_value++ )
{
if ( $c_value <= 1 ) {
$next_value = $c_value;
} else {
$next_value = $first_value + $second_value;
$first_value = $second_value;
$second_value = $next_value;
}
$letters[] = $str_split[$next_value];
}
echo implode($letters, "-");
输出:
T-h-h-e-D-V-c-e-m-T
在此处运行它:
下面是一个有趣的PHP斐波那契参考线程:
编辑:此代码将返回未定义的索引,因为它在超过strlen时不会停止
进行了与@mkaatman类似的编辑
这是我的3v4l:
编辑:此代码将返回一个未定义的索引,因为它在超过strlen时不会停止
进行了与@mkaatman类似的编辑
这是我的3v4l:
您的斐波那契级数生成代码不正确,请尝试以下操作: 更新: 一旦该代码的总和超出消息长度范围,它将停止
$message = 'The Da Vinci Code is a 2003 mystery-detective novel by Dan Brown';
// remove all the spaces from message
$message = str_replace(' ', '', $message);
$str_split = str_split($message);
$x = 0;
$y = 1;
$next = 0;
//stopping the loop if the character index goes out of range
for($i=1;$next<=count($str_split);$i++)
{
if($i == 1) //for first element use 0 as the sum
{
$farray[] = $x;
continue;
}
if($i == 2) //for second element use 1 as the sum
{
$farray[] = $y;
continue;
}
$next = $x + $y;
$x=$y;
$y=$next;
$farray[] = $next;
}
foreach($farray as $key=>$fvalue){
echo $str_split[$fvalue]."--";
}
原始答复:-
它的工作原理是:-
您的斐波那契级数生成代码不正确,请尝试以下操作: 更新: 一旦该代码的总和超出消息长度范围,它将停止
$message = 'The Da Vinci Code is a 2003 mystery-detective novel by Dan Brown';
// remove all the spaces from message
$message = str_replace(' ', '', $message);
$str_split = str_split($message);
$x = 0;
$y = 1;
$next = 0;
//stopping the loop if the character index goes out of range
for($i=1;$next<=count($str_split);$i++)
{
if($i == 1) //for first element use 0 as the sum
{
$farray[] = $x;
continue;
}
if($i == 2) //for second element use 1 as the sum
{
$farray[] = $y;
continue;
}
$next = $x + $y;
$x=$y;
$y=$next;
$farray[] = $next;
}
foreach($farray as $key=>$fvalue){
echo $str_split[$fvalue]."--";
}
原始答复:-
它的工作原理是:-
你的代码几乎是正确的 您将删除空格并添加两个数组的起始元素
$message = str_replace( ' ', '', 'The Da Vinci Code is a 2003 mystery-detective novel by Dan Brown' );
$str_split = str_split( $message );
$x = 0;
$y = 1;
$farray[] = $x;
$farray[] = $y;
for ( $i = 2; $i <= 10; $i ++ ) {
$z = $x + $y;
$farray[] = $z;
$x = $y;
$y = $z;
}
foreach ( $farray as $key => $fvalue ) {
echo $str_split[ $fvalue ] . "-";
}
你的代码几乎是正确的 您将删除空格并添加两个数组的起始元素
$message = str_replace( ' ', '', 'The Da Vinci Code is a 2003 mystery-detective novel by Dan Brown' );
$str_split = str_split( $message );
$x = 0;
$y = 1;
$farray[] = $x;
$farray[] = $y;
for ( $i = 2; $i <= 10; $i ++ ) {
$z = $x + $y;
$farray[] = $z;
$x = $y;
$y = $z;
}
foreach ( $farray as $key => $fvalue ) {
echo $str_split[ $fvalue ] . "-";
}
最短的可能方法是使用preg_替换和echo字符串位置
<?php
$message = 'The Da Vinci Code is a 2003 mystery-detective novel by Dan Brown';
$message = preg_replace('/\s+/', '', $message);
$strlenggth = strlen($message);
echo $message[$x];
$x = 0;
$y = 1;
for($i=0;$x<=$strlenggth;$i++)
{
$z = $x + $y;
$farray[] = $z;
$x=$y;
$y=$z;
echo "-".$message[$x];
}
?>
最短的可能方法是使用preg_替换和echo字符串位置
<?php
$message = 'The Da Vinci Code is a 2003 mystery-detective novel by Dan Brown';
$message = preg_replace('/\s+/', '', $message);
$strlenggth = strlen($message);
echo $message[$x];
$x = 0;
$y = 1;
for($i=0;$x<=$strlenggth;$i++)
{
$z = $x + $y;
$farray[] = $z;
$x=$y;
$y=$z;
echo "-".$message[$x];
}
?>
或者,您可以使用递归来避免使用太多for循环:
$message = str_replace(" ", "", $message);
for($i = 0; $i < 10; $i++) {
echo substr($message, getNthValue($i), 1);
if($i != 9) {
echo "-";
}
}
function getNthValue($n) {
if($n <= 1) {
return $n;
}
if($n > 1) {
return getNthValue($n-1) + getNthValue($n-2);
}
}
或者,您可以使用递归来避免使用太多for循环:
$message = str_replace(" ", "", $message);
for($i = 0; $i < 10; $i++) {
echo substr($message, getNthValue($i), 1);
if($i != 9) {
echo "-";
}
}
function getNthValue($n) {
if($n <= 1) {
return $n;
}
if($n > 1) {
return getNthValue($n-1) + getNthValue($n-2);
}
}
递归版本并封装在函数中,字符串长度应为irrilevant:
function fiboSecret($msg, $num = 0, $fib = [], $secret = []){
if(count($fib) > 0) {
if($num == 1){
if(strlen($msg) > 1){
$secret[] = strtoupper($msg[1]);
return fiboSecret($msg, ++$num, array(0, 1), $secret);
}else{
return $secret;
}
}
$lastFibo = $fib[count($fib) - 1];
if(array_key_exists($lastFibo, str_split($msg))){
$secret[] = strtoupper($msg[$lastFibo]);
$fib[] = $fib[$num-1] + $fib[$num-2];
return fiboSecret($msg, ++$num, $fib, $secret);
}else{
return $secret;
}
}else if(strlen($msg) > 0){
$msg = preg_replace('/\s+/', '', $msg);
$secret[] = strtoupper($msg[0]);
return fiboSecret($msg, ++$num, array(0), $secret);
}else{
return [];
}
if(array_key_exists($lastFibo, str_split($msg))){
$secret[] = strtoupper($msg[$lastFibo]);
$num++;
}
}
递归版本并封装在函数中,字符串长度应为irrilevant:
function fiboSecret($msg, $num = 0, $fib = [], $secret = []){
if(count($fib) > 0) {
if($num == 1){
if(strlen($msg) > 1){
$secret[] = strtoupper($msg[1]);
return fiboSecret($msg, ++$num, array(0, 1), $secret);
}else{
return $secret;
}
}
$lastFibo = $fib[count($fib) - 1];
if(array_key_exists($lastFibo, str_split($msg))){
$secret[] = strtoupper($msg[$lastFibo]);
$fib[] = $fib[$num-1] + $fib[$num-2];
return fiboSecret($msg, ++$num, $fib, $secret);
}else{
return $secret;
}
}else if(strlen($msg) > 0){
$msg = preg_replace('/\s+/', '', $msg);
$secret[] = strtoupper($msg[0]);
return fiboSecret($msg, ++$num, array(0), $secret);
}else{
return [];
}
if(array_key_exists($lastFibo, str_split($msg))){
$secret[] = strtoupper($msg[$lastFibo]);
$num++;
}
}
你的产出是多少?这是您的第一个问题,它没有正确匹配,您从未存储0,因为您的第一个存储值是0+1中的1。我已解决此问题以匹配您的答案您在计算字符时没有考虑空格,因此请查看。答案完全符合您的要求。您的输出是什么?这是您的第一个问题,它没有正确匹配,您从未存储0,因为您的第一个存储值是0+1中的1。我已解决此问题以匹配您的答案您在计算字符时没有考虑空格,因此请查看。答案完全符合你的要求。我得到了一个未定义的偏移量:我的55包括你的str替换,你的怎么不会触发它?@brianforan不知道,但我会重新检查它。@brianforan我已经更新了我的代码,现在应该可以防止这些错误了,你能从你这边确认一下吗。提前谢谢我得到了一个未定义的偏移量:我的55包括你的str替换,为什么你的不触发它?@brianforan不知道,但我会重新检查它。@brianforan我已经更新了我的代码,现在应该可以防止这些错误了,你能从你这边确认一下吗。提前感谢预期输出是正确的。问题说明应该忽略空白。我已经更新了我的答案,以符合他的确切要求,并返回了他的确切结果。预期的输出是正确的。这个问题说明应该忽略空格。我已经更新了我的答案,以符合他的确切要求,并返回了他的确切结果。