doucuan5365 2017-06-07 13:31
浏览 75
已采纳

我怎样才能在php中实现FibonacciSecret

I'm trying to get all the characters in that message at positions that are present in the Fibonacci sequence (a sequence formed by the the Fibonacci number sorted in ascending order). Please ignore whitespace characters and use the extended Fibonacci.

Return the obtained characters capitalized and connected by the '-' character.

Example

For message = "The Da Vinci Code is a 2003 mystery-detective novel by Dan Brown",

the output should be

FibonacciSecret(message) = "T-H-H-E-D-V-C-E-M-T".

The first Fibonacci is 0 then the first letter is T

The second Fibonacci is 1 then the second letter is H

The third Fibonacci is 1 then the third letter is H ... and so on.

Thus, the answer should be "T-H-H-E-D-V-C-E-M-T".

Tried Code

<?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]."-";
}

?>

Output

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-

Expected Output

the answer should be "T-H-H-E-D-V-C-E-M-T".

Can anyone tell me. Where im going wrong in this one ?

  • 写回答

7条回答 默认 最新

  • duanpendan8067 2017-06-07 13:52
    关注

    Your Fibonacci series generation code was not correct, Try this:

    Update:

    This code will stop once it sum goes out of the range of message length.

    $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]."--";
    }
    

    Original Answer:-

    It Works:-

    $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]."--";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况