如何优化这段代码?

时间:2010-09-26 06:36:46

标签: php optimization puzzle

我在PHP中解决了以下问题。 但是执行 10位数字需要花费太多时间。我想知道我哪里出错了? 我是动态编程的新手。有人看看这个吗?

问题

在Byteland,他们有一个非常奇怪的货币体系。

每个Bytelandian金币上都写有整数。一枚硬币 可以在银行兑换成三个硬币:n / 2,n / 3和n / 4。 但这些数字都是向下舍入的(银行必须赚钱)。

您还可以出售美元的Bytelandian硬币。找的零钱 费率是1:1。但你不能买Bytelandian硬币。

你有一枚金币。美元的最高金额是多少? 你能得到它吗?

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

<?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 \n";
 }
?>

2 个答案:

答案 0 :(得分:2)

这里有一个重新格式化的代码版本(如我)可以理解上述内容。它没有改善任何东西。

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 :(得分:0)

我仍然有我的代码来自这个问题。但它是用c ++编写的。它绝不是最有效的,但它通过了。移植到php可能并不太难。


#include <cstdio>
#include <queue>
using namespace std;

unsigned long results;
queue to_test;

int main()
{
  char tmp_val[30];
  unsigned long coin_value = 1;
  while (coin_value)
  {
    scanf("%s", tmp_val);
    coin_value = 0;
    results = 0;
    for (int w = 0; tmp_val[w] != '\0'; w++)
    {
      coin_value *= 10;
      coin_value += tmp_val[w] - 0x30;
    }
    if (coin_value != 0)
    {
      to_test.push(coin_value);
      while(!to_test.empty())
      {
        unsigned long tester = to_test.front();
        to_test.pop();
        unsigned long over2 = tester/2;
        unsigned long over3 = tester/3;
        unsigned long over4 = tester/4;
        if (tester < over2 + over3 + over4)
        {
          to_test.push(over2);
          to_test.push(over3);
          to_test.push(over4);
        }
        else
        {
          results += tester;
        }
      }
      printf("%lu\n", results);
    }
  }
}


相关问题