如何优化这段代码?

发布于 2024-09-25 02:51:26 字数 1809 浏览 2 评论 0原文

我有一个 PHP 解决方案来解决以下问题。 但对于 10 位数字执行起来需要太多时间。我想知道我哪里出错了? 我是动态编程的新手。有人可以看一下吗?

问题

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

每一枚 Bytelandian 金币上都写有一个整数。一枚硬币n 可以在银行兑换成三种硬币: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";
 }
?>

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

说谎友 2024-10-02 02:51:26

这是代码的重新格式化版本,供那些能够理解上述内容的人(例如我)使用。它没有改善任何东西。

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);
}

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);
}
优雅的叶子 2024-10-02 02:51:26

我仍然有这个问题的代码。但它是用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);
    }
  }
}


I still have my code from this problem. But it's in c++. It's by no means the most efficient, but it passed. It might not be too hard to port to 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);
    }
  }
}


~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文