硬币改变自下而上动态编程

时间:2013-04-16 14:59:12

标签: c++ algorithm dynamic-programming coin-change bottom-up

http://uva.onlinejudge.org/external/6/674.html我正试图解决这个问题。但请注意,这不是最小的硬币更换问题,它要求我使用50,25,15,10,5和1美分硬币制作N美分的不同方法。这很简单,所以我做了这个功能:

int count(int n, int m) // n is the N of the problem, m is the number of coin types and s[] is {1, 5, 10, 25, 50}
{
  if (n == 0)
  {
    return 1;
  }

  if (n < 0)
  {
    return 0;
  }

  if (m < 0 && n >= 1)
  {
    return 0;
  }

  return DP[n][m - 1] + DP[n - s[m]][m];
}

使用memoization添加动态编程也相当简单:

int count(int n, int m)
{
  if (n == 0)
  {
    return 1;
  }

  if (n < 0)
  {
    return 0;
  }

  if (m < 0 && n >= 1)
  {
    return 0;
  }

  if (DP[n][m - 1] == -1 || DP[n - s[m]][m] == -1)
  {
    return count(n, m - 1) + count(n - s[m], m);
  }
  else
  {
    return DP[n][m - 1] + DP[n - s[m]][m];
  }
}

然而,这些都不够快 - 我需要自下而上的动态编程,但我在编码时遇到了困难,即使在Algorithmist的帮助下 - http://www.algorithmist.com/index.php/Coin_Change

void generate()
{
  for (i = 0; i < MAX; i++)
  {
    for (u = 0; u < m; u++)
    {
      if (i == 0)
      {
        DP[i][u] = 1;
      }
      else if (u == 0)
      {
        DP[i][u] = 0;
      }
      else if (s[u] > i)
      {
        DP[i][u] = DP[i][u - 1];
      }
      else
      {
        DP[i][u] = DP[i][u - 1] + DP[i - s[u]][u];
      }
    }
  }
}

由于某种原因,我得到每个结果0,这是我的完整代码:

#include <stdio.h>
#include <string.h>

using namespace std;

#define MAX 7490

int s[] = {1, 5, 10, 25, 50}, m = 5, input, DP[MAX][5], i, u;

int count(int n, int m)
{
  if (n == 0)
  {
    return 1;
  }

  if (n < 0)
  {
    return 0;
  }

  if (m < 0 && n >= 1)
  {
    return 0;
  }

  if (DP[n][m - 1] == -1 || DP[n - s[m]][m] == -1)
  {
    return count(n, m - 1) + count(n - s[m], m);
  }
  else
  {
    return DP[n][m - 1] + DP[n - s[m]][m];
  }
}

void generate()
{
  for (i = 0; i < MAX; i++)
  {
    for (u = 0; u < m; u++)
    {
      if (i == 0)
      {
        DP[i][u] = 1;
      }
      else if (u == 0)
      {
        DP[i][u] = 0;
      }
      else if (s[u] > i)
      {
        DP[i][u] = DP[i][u - 1];
      }
      else
      {
        DP[i][u] = DP[i][u - 1] + DP[i - s[u]][u];
      }
    }
  }
}

int main()
{
  memset(DP, -1, sizeof DP);
  generate();

  while (scanf("%d", &input) != EOF)
  {
    //printf("%d\n", count(input, 4));
    printf("%d\n", DP[input][4]);
  }

  return 0;
}

1 个答案:

答案 0 :(得分:2)

你在这里犯了错误:

else if (u == 0)
{
   DP[i][u] = 0;
}

它应该是DP[i][u]=1,因为您可以使用1美分硬币以一种可能的方式生成任何值i。即拿5美分,你需要5美分硬币,这是一种总共5美分的方法。

-----

顺便说一句,在你计算方法的第一种方法中,你有这个:

if (DP[n][m - 1] == -1 || DP[n - s[m]][m] == -1)
{
  return count(n, m - 1) + count(n - s[m], m);
}

或者这个:

if (DP[n][m - 1] == -1 || DP[n - s[m]][m] == -1)
{
    return DP[n][m] = count(n, m - 1) + count(n - s[m], m);
}

如果你没有记住已计算的结果,那么这个记忆检查if (DP[n][m - 1] == -1 || DP[n - s[m]][m] == -1)将永远不会起作用,这可能是你的第一种方法过慢的原因: - ?

相关问题