我正在尝试将该消息中的所有字符放在Fibonacci序列中存在的位置(由斐波那契数字按升序排序的序列)。请忽略空白字符并使用扩展的Fibonacci。
返回由“ - ”字符大写并连接的获取字符。
示例
For message =“达芬奇密码是Dan Brown的2003年神秘侦探小说”,
输出应为
FibonacciSecret(消息)=“T-H-H-E-D-V-C-E-M-T”。
第一个Fibonacci为0,然后第一个字母为T
第二个Fibonacci是1然后第二个字母是H
第三个Fibonacci是1然后第三个字母是H ......依此类推。
因此,答案应为“T-H-H-E-D-V-C-E-M-T”。
尝试过代码
<?php
$message = 'The Da Vinci Code is a 2003 mystery-detective novel by Dan Brown';
$str_split = str_split($message);
$x = 0;
$y = 1;
for($i=0;$i<=10;$i++)
{
$z = $x + $y;
$farray[] = $z;
$x=$y;
$y=$z;
}
foreach($farray as $key=>$fvalue){
echo $str_split[$fvalue]."-";
}
?>
输出
h-T-
E_NOTICE : type 8 -- Undefined offset: -1 -- at line 18
-
E_NOTICE : type 8 -- Undefined offset: -1 -- at line 18
-T-h-h-T-
E_NOTICE : type 8 -- Undefined offset: -1 -- at line 18
-
E_NOTICE : type 8 -- Undefined offset: -1 -- at line 18
-T-
预期输出
答案应为“T-H-H-E-D-V-C-E-M-T”。
任何人都可以告诉我。哪一个我在这个错了?
答案 0 :(得分:2)
你的Fibonacci系列代码不正确,试试这个:
<强>更新强>
一旦总和超出消息长度范围,此代码将停止。
$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;
for($i=1;$i<=10;$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]."--";
}
答案 1 :(得分:1)
我认为你的Fibonacci逻辑是关闭的。我找到了一个我在这里找到的例子: https://www.easycalculation.com/code-php-program-fibonacci-series.html
<强>序列:强> 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
在此处运行: http://sandbox.onlinephpfunctions.com/code/45c72df556c19e239e09ba0e334ace1e40ce809c
这是PHP fibonacci的一个有趣的参考线程: PHP Fibonacci Sequence
答案 2 :(得分:1)
编辑:此代码将返回未定义的索引,因为它超出strlen时不会停止
进行与@mkaatman
类似的编辑这是我的3v4l:https://3v4l.org/5KIZD
<?php
$message = 'The Da Vinci Code is a 2003 mystery-detective novel by Dan Brown';
$message = str_replace(' ', '', $message);
$str_split = str_split($message);
$x = 0;
$y = 1;
$farray[0] = 0;
$farray[1] = 1;
for($i=2;$i<=10;$i++)
{
$z = $x + $y;
$farray[] = $z;
$x=$y;
$y=$z;
}
foreach($farray as $key=>$fvalue){
//echo $key . " => " . $fvalue . "\n";
echo $str_split[$fvalue]."-";
}
?>
答案 3 :(得分:1)
您的代码几乎是正确的。
您将删除空格并添加两个数组的起始元素。
$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 ] . "-";
}
答案 4 :(得分:1)
最短的方法是使用preg_replace和echo string position;
<?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];
}
?>
答案 5 :(得分:1)
或者,您可以使用递归来避免使用过多的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);
}
}
答案 6 :(得分:1)
递归版本并包装在一个函数中,字符串长度应该是不相关的:
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++;
}
}