Algorithm 最小硬币兑换打印所有组合

Algorithm 最小硬币兑换打印所有组合,algorithm,dynamic-programming,Algorithm,Dynamic Programming,给定一组硬币面额硬币和一个总数,找出所有可能的组合,使硬币总数达到最小。在我的解决方案中,我在表[I]中记录了总计为I的最小硬币数。我不确定这将如何修改以存储总计为I的实际硬币,并确保在这种情况下包括这两种可能性。我看过其他关于堆栈溢出的代码,但我只找到可以打印任何一个最佳解决方案的代码 输入:最小硬币(10,[2,3,5,6,7,8])输出:[[5,5],[2,8]] INT_MAX = 2 ** 63 - 1 def minimum_coins(total, coins) table =

给定一组硬币面额
硬币
和一个
总数
,找出所有可能的组合,使硬币总数达到最小。在我的解决方案中,我在
表[I]
中记录了总计为
I
的最小硬币数。我不确定这将如何修改以存储总计为
I
的实际硬币,并确保在这种情况下包括这两种可能性。我看过其他关于堆栈溢出的代码,但我只找到可以打印任何一个最佳解决方案的代码

输入:最小硬币(10,[2,3,5,6,7,8])
输出:[[5,5],[2,8]]

INT_MAX = 2 ** 63 - 1
def minimum_coins(total, coins)
  table = Array.new(total + 1)
  table[0] = 0

  (1..total).to_a.each do |i|
    table[i] = INT_MAX
  end

  (1..total).to_a.each do |i|
    (0..coins.length-1).to_a.each do |j|
      if coins[j] <= i
        sub_res = table[i-coins[j]]
        if sub_res != INT_MAX && sub_res + 1 < table[i]
          table[i] = sub_res + 1
        end
      end
    end
  end
  puts table.inspect
end

minimum_coins(10, [2,3,5,6,7,8])
INT_MAX=2**63-1
def最小硬币数(总硬币数)
表=数组。新(总计+1)
表[0]=0
(1.总计)给a.每人|
表[i]=INT_MAX
结束
(1.总计)给a.每人|
(0.硬币,长度-1)到a.每个都做|

如果硬币[j]让我们来看看
条目包含成对的
(最佳计数,最新硬币列表)

如果
sub_res+1
BestCount
替换为
sub_res+1
并制作
lastconlist
包含
coins[j]

如果
sub_res+1=表[i].BestCount
只需将
coins[j]
值添加到
LastCoinList

因此,最后,
表[10]
将包含
BestValue=2
lastconlist=(5,7,8)

现在递归地从
表[10]
条目展开到
表[10-5]
,再到
表[10-7]
,再到
表[10-8]
,分别包含5,3和2,然后所有递归分支在第0个条目处停止。

d[i] = minimum changes for i
因此,如果
d[i]==d[i-c]+1
,我们可以说,如果我们用硬币
c
进行交换,我们仍然可以为i得到最小的硬币兑换

代码:


如果您还有一些问题,请不要犹豫,在这里留下评论。

我制作了一个迭代程序,可以计算出获得给定数量的美分所需的最小硬币数量(美元)。它是迭代的,但希望它有帮助

import java.util.*;
public class CoinProblem
{
public static void main(String[] args)
{
  System.out.println("----------------------------COIN PROBLEM--------------------------");
  System.out.println("Denominations: \nCent - 1\nNickel - 5\nDime - 10\nQuarter - 25");
  Map<Integer, Integer> map = new HashMap<Integer, Integer> ();
  System.out.println("\nENTER TARGET NUMBER (CENTS): ");
  Scanner sc = new Scanner(System.in);
  int target = Integer.parseInt(sc.next());
  int count = numCoins(target, map);
  System.out.println("\nMINIMUM NUMBER OF COINS REQUIRED: " + count);
  System.out.println( map.get(1) + " CENTS");
  System.out.println( map.get(5) + " NICKELS"); 
  System.out.println( map.get(10) + " DIMES");
  System.out.println( map.get(25) + " QUARTERS");
  System.out.println("------------------------------------------------------------------");    
}
public static int numCoins(int target, Map<Integer, Integer> map)
{
  int cent = 1;
  int nickel = 5;
  int dime = 10;
  int quarter = 25;
  map.put(cent, 0);
  map.put(nickel, 0);
  map.put(dime, 0);
  map.put(quarter, 0);
  int count = 0;
  if (target >= 25)
  {
     if (target % 25 == 0)
     {
        count += target/25;
        map.put(quarter, count);
        return count;
     }
     else
     {
        count += target/25;
        map.put(quarter, count);
        int remtarget = target%25;
        if (remtarget >= 10)
        {
           if (remtarget %  10 == 0)
           {
              count += remtarget/10;
              map.put(dime, remtarget/10);
              return count;
           }
           else
           {
              count += remtarget/10;
              map.put(dime, remtarget/10);
              int fivetarget = remtarget%10;
              if (fivetarget >= 5)
              {
                 if (fivetarget % 5 == 0)
                 {
                    count += fivetarget/5;
                    map.put(nickel, fivetarget/5);
                 }
                 else
                 {
                    count += fivetarget/5;
                    map.put(nickel, fivetarget/5);
                    int ones = fivetarget%5;
                    count+= ones;
                    map.put(cent, ones);
                 }
              }
              else
              {
                 count += fivetarget;
                 map.put(cent, fivetarget);
                 return count;
              }
           }
        }
        else
        {
           if (remtarget >= 5)
           {
              if (remtarget % 5 == 0)
              {
                 count += remtarget/5;
                 map.put(nickel, remtarget/5);
              }
              else
              {
                 count += remtarget/5;
                 map.put(nickel, remtarget/5);
                 int ones = remtarget%5;
                 count+= ones;
                 map.put(cent, ones);
              }
           }
           else
           {
              count += remtarget;
              map.put(cent, remtarget);
              return count;
           }

        }
     }
  }
  else
  {
     if (target == 0)
     {
        return 0;
     }
     if (target >= 10)
     {
        if (target %  10 == 0)
        {
           count += target/10;
           map.put(dime, target/10);
           return count;
        }
        else
        {
           count += target/10;
           map.put(dime, target/10);
           int ftarget = target%10;           
           if (ftarget >= 5)
           {
              if (ftarget % 5 == 0)
              {
                 count += ftarget/5;
                 map.put(nickel, ftarget/5);
              }
              else
              {
                 count += ftarget/5;
                 map.put(nickel, ftarget/5);
                 int otarget = ftarget%5;
                 count+= otarget;
                 map.put(cent, otarget);
              }
           }
           else
           {
              count += ftarget;
              map.put(cent, ftarget);
              return count;
           }
        }
     }
     else
     {  
        if (target > 5)
        {
           if (target % 5 == 0)
           {
              count += target/5;
              map.put(nickel, target/5);
           }
           else
           {
              count += target/5;
              map.put(nickel, target/5);
              int o = target%5;
              count+= o;
              map.put(cent, o);
           }
        }
        else
        {
           count += target;
           map.put(cent, target);
           return count;
        }
     }
  }
  return count;
 }
 }
import java.util.*;
公共类问题
{
公共静态void main(字符串[]args)
{
System.out.println(“-------------------------------------硬币问题-----------------------------------------”);
System.out.println(“面额:\nCent-1\nNickel-5\nDime-10\nCharter-25”);
Map Map=newhashmap();
System.out.println(“\n输入目标编号(美分):”;
扫描仪sc=新的扫描仪(System.in);
int target=Integer.parseInt(sc.next());
int count=numCoins(目标,地图);
System.out.println(“\n所需硬币的最小数量:”+计数);
System.out.println(map.get(1)+“美分”);
System.out.println(map.get(5)+“镍币”);
System.out.println(map.get(10)+“DIMES”);
System.out.println(map.get(25)+“QUARTERS”);
System.out.println(“---------------------------------------------------------------------------”);
}
公共静态int numCoins(int目标,映射)
{
整数=1;
int镍=5;
int-dime=10;
国际季度=25;
地图投入(分,0);
map.put(镍,0);
地图放置(一角,0);
地图放置(四分之一,0);
整数计数=0;
如果(目标>=25)
{
如果(目标%25==0)
{
计数+=目标/25;
地图放置(四分之一,计数);
返回计数;
}
其他的
{
计数+=目标/25;
地图放置(四分之一,计数);
int remtarget=目标%25;
如果(remtarget>=10)
{
如果(目标%10==0)
{
计数+=remtarget/10;
地图。放置(一角,remtarget/10);
返回计数;
}
其他的
{
计数+=remtarget/10;
地图。放置(一角,remtarget/10);
int fivetarget=remtarget%10;
如果(五个目标>=5)
{
如果(五个目标%5==0)
{
计数+=五个目标/5;
map.put(镍,五个目标/5);
}
其他的
{
计数+=五个目标/5;
map.put(镍,五个目标/5);
整数=五个目标%5;
计数+=个;
地图放置(分,个);
}
}
其他的
{
计数+=五个目标;
地图放置(分,五个目标);
返回计数;
}
}
}
其他的
{
如果(remtarget>=5)
{
如果(目标%5==0)
{
计数+=remtarget/5;
map.put(镍,remtarget/5);
}
其他的
{
计数+=remtarget/5;
map.put(镍,remtarget/5);
整数=目标%5;
计数+=个;
地图放置(分,个);
}
}
其他的
{
计数+=remtarget;
map.put(分,remtarget);
返回计数;
}
}
}
}
其他的
{
如果(目标==0)
{
返回0;
}
如果(目标>=10)
{
如果(目标%10==0)
{
计数+=目标/10;
地图放置(一角,目标/10);
返回计数;
}
其他的
{
计数+=目标/10;
地图放置(一角,目标/10);
int ftarget=目标%10;
如果(ftarget>=5)
{
如果(目标%5==0)
{
计数+=ftarget/5;
map.put(镍,ftarget/5);
}
其他的
{
计数+=ftarget/5;
map.put(镍,ftarget/5);
int-otarget=ftarget%5;
计数+=otarget;
import java.util.*;
public class CoinProblem
{
public static void main(String[] args)
{
  System.out.println("----------------------------COIN PROBLEM--------------------------");
  System.out.println("Denominations: \nCent - 1\nNickel - 5\nDime - 10\nQuarter - 25");
  Map<Integer, Integer> map = new HashMap<Integer, Integer> ();
  System.out.println("\nENTER TARGET NUMBER (CENTS): ");
  Scanner sc = new Scanner(System.in);
  int target = Integer.parseInt(sc.next());
  int count = numCoins(target, map);
  System.out.println("\nMINIMUM NUMBER OF COINS REQUIRED: " + count);
  System.out.println( map.get(1) + " CENTS");
  System.out.println( map.get(5) + " NICKELS"); 
  System.out.println( map.get(10) + " DIMES");
  System.out.println( map.get(25) + " QUARTERS");
  System.out.println("------------------------------------------------------------------");    
}
public static int numCoins(int target, Map<Integer, Integer> map)
{
  int cent = 1;
  int nickel = 5;
  int dime = 10;
  int quarter = 25;
  map.put(cent, 0);
  map.put(nickel, 0);
  map.put(dime, 0);
  map.put(quarter, 0);
  int count = 0;
  if (target >= 25)
  {
     if (target % 25 == 0)
     {
        count += target/25;
        map.put(quarter, count);
        return count;
     }
     else
     {
        count += target/25;
        map.put(quarter, count);
        int remtarget = target%25;
        if (remtarget >= 10)
        {
           if (remtarget %  10 == 0)
           {
              count += remtarget/10;
              map.put(dime, remtarget/10);
              return count;
           }
           else
           {
              count += remtarget/10;
              map.put(dime, remtarget/10);
              int fivetarget = remtarget%10;
              if (fivetarget >= 5)
              {
                 if (fivetarget % 5 == 0)
                 {
                    count += fivetarget/5;
                    map.put(nickel, fivetarget/5);
                 }
                 else
                 {
                    count += fivetarget/5;
                    map.put(nickel, fivetarget/5);
                    int ones = fivetarget%5;
                    count+= ones;
                    map.put(cent, ones);
                 }
              }
              else
              {
                 count += fivetarget;
                 map.put(cent, fivetarget);
                 return count;
              }
           }
        }
        else
        {
           if (remtarget >= 5)
           {
              if (remtarget % 5 == 0)
              {
                 count += remtarget/5;
                 map.put(nickel, remtarget/5);
              }
              else
              {
                 count += remtarget/5;
                 map.put(nickel, remtarget/5);
                 int ones = remtarget%5;
                 count+= ones;
                 map.put(cent, ones);
              }
           }
           else
           {
              count += remtarget;
              map.put(cent, remtarget);
              return count;
           }

        }
     }
  }
  else
  {
     if (target == 0)
     {
        return 0;
     }
     if (target >= 10)
     {
        if (target %  10 == 0)
        {
           count += target/10;
           map.put(dime, target/10);
           return count;
        }
        else
        {
           count += target/10;
           map.put(dime, target/10);
           int ftarget = target%10;           
           if (ftarget >= 5)
           {
              if (ftarget % 5 == 0)
              {
                 count += ftarget/5;
                 map.put(nickel, ftarget/5);
              }
              else
              {
                 count += ftarget/5;
                 map.put(nickel, ftarget/5);
                 int otarget = ftarget%5;
                 count+= otarget;
                 map.put(cent, otarget);
              }
           }
           else
           {
              count += ftarget;
              map.put(cent, ftarget);
              return count;
           }
        }
     }
     else
     {  
        if (target > 5)
        {
           if (target % 5 == 0)
           {
              count += target/5;
              map.put(nickel, target/5);
           }
           else
           {
              count += target/5;
              map.put(nickel, target/5);
              int o = target%5;
              count+= o;
              map.put(cent, o);
           }
        }
        else
        {
           count += target;
           map.put(cent, target);
           return count;
        }
     }
  }
  return count;
 }
 }