duandao2083 2010-09-26 06:36
浏览 8
已采纳

如何优化这段代码?

I have a solution to the below problem in PHP. But it is taking too much time to execute for 10 digit numbers. I want to know where am I going wrong ? I am new to dynamic programming .Can someone have a look at this ?

Problem

In Byteland they have a very strange monetary system.

Each Bytelandian gold coin has an integer number written on it. A coin n can be exchanged in a bank into three coins: n/2, n/3 and n/4. But these numbers are all rounded down (the banks have to make a profit).

You can also sell Bytelandian coins for American dollars. The exchange rate is 1:1. But you can not buy Bytelandian coins.

You have one gold coin. What is the maximum amount of American dollars you can get for it?

========================================================

<?php 
$maxA=array();

function exchange($money)
{
        if($money == 0)
        {
               return $money;
        }
        if(isset($maxA[$money]))
        {
                $temp = $maxA[$money]; // gets the maximum dollars for N
        }
        else
        {
           $temp = 0;
        }
        if($temp == 0)
        {
            $m = $money/2;
            $m = floor($m);
            $o = $money/3;
            $o = floor($o);
            $n = $money/4;
            $n = floor($n);
            $total = $m+$n+$o;

       if(isset($maxA[$m]))
        {
             $m = $maxA[$m];

        }
        else
        {
           $m = exchange($m);
        }
        if(isset($maxA[$n]))
        {
         $n = $maxA[$n];
        }
        else
        {
           $n = exchange($n);
        }
        if(isset($maxA[$o]))
        {
           $o = $maxA[$o];
        }
        else
        {
           $o = exchange($o);
        }

       $temp = max($total,$m+$n+$o,$money);
       $maxA[$money]=$temp;  //store the value
        }

return $temp; 
}

$A=array();
while(1)
{
      $handle = fopen ("php://stdin","r");
      $line = fgets($handle);
      if(feof($handle))
      {
          break;
      }
      array_push($A,trim($line));
}

$count =count($A);
for($i=0;$i<$count;$i++)
{
      $val = exchange($A[$i]);
      print "$val 
";
 }
?>
  • 写回答

2条回答 默认 最新

  • douwen8118 2010-09-26 07:02
    关注

    Here a reformatted version of the code for the ones (like I) who could understand the above. It doesn't improve anything.

    function exchange($money) {
        static $maxA = array(0 => 0);
    
        if (isset($maxA[$money])) {
            return $money;
        }
    
        $m = floor($money/2);
        $o = floor($money/3);
        $n = floor($money/4);
        $total = $m+$n+$o;
    
        if (isset($maxA[$m])) {
            $m = $maxA[$m];
        } else {
            $m = exchange($m);
        }
    
        if (isset($maxA[$n])) {
            $n = $maxA[$n];
        } else {
            $n = exchange($n);
        }
        if (isset($maxA[$o])) {
            $o = $maxA[$o];
        } else {
            $o = exchange($o);
        }
    
        return $maxA[$money] = max($total, $m + $n + $o, $money);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示