- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
比如总金额应该是5,我有1和2的硬币,那么有3种组合方式:
1 1 1 1 1
1 1 1 2
1 2 2
我看过一些关于如何使用动态规划或递归计算组合总数的帖子,但我想像上面的示例一样输出所有组合。我在下面提出了一个递归解决方案。
这基本上是一种回溯算法,我首先从最小的硬币开始并尝试获得总数,然后我删除一些硬币并尝试使用第二小的硬币......您可以在下面的 http://cpp.sh/ 中运行我的代码
在我的代码中,总数是 10,可用的硬币值是 1、2、5。
#include <iostream>
#include <stdlib.h>
#include <iomanip>
#include <cmath>
#include <vector>
using namespace std;
vector<vector<int>> res;
vector<int> values;
int total = 0;
void helper(vector<int>& curCoins, int current, int i){
int old = current;
if(i==values.size())
return;
int val = values[i];
while(current<total){
current += val;
curCoins.push_back(val);
}
if(current==total){
res.push_back(curCoins);
}
while (current>old) {
current -= val;
curCoins.pop_back();
if (current>=0) {
helper(curCoins, current, i+1);
}
}
}
int main(int argc, const char * argv[]) {
total = 10;
values = {1,2,5};
vector<int> chosenCoins;
helper(chosenCoins, 0, 0);
cout<<"number of combinations: "<<res.size()<<endl;
for (int i=0; i<res.size(); i++) {
for (int j=0; j<res[i].size(); j++) {
if(j!=0)
cout<<" ";
cout<<res[i][j];
}
cout<<endl;
}
return 0;
}
是否有更好的解决方案来输出该问题的所有组合?动态规划?
编辑:
我的问题是这个问题可以用动态规划解决吗?
感谢您的帮助。我在这里实现了 DP 版本:Coin Change DP Algorithm Print All Combinations
最佳答案
DP 解决方案:
我们有
{solutions(n)} = Union ({solutions(n - 1) + coin1},
{solutions(n - 2) + coin2},
{solutions(n - 5) + coin5})
所以在代码中:
using combi_set = std::set<std::array<int, 3u>>;
void append(combi_set& res, const combi_set& prev, const std::array<int, 3u>& values)
{
for (const auto& p : prev) {
res.insert({{{p[0] + values[0], p[1] + values[1], p[2] + values[2]}}});
}
}
combi_set computeCombi(int total)
{
std::vector<combi_set> combis(total + 1);
combis[0].insert({{{0, 0, 0}}});
for (int i = 1; i <= total; ++i) {
append(combis[i], combis[i - 1], {{1, 0, 0}});
if (i - 2 >= 0) { append(combis[i], combis[i - 2], {{0, 1, 0}}); }
if (i - 5 >= 0) { append(combis[i], combis[i - 5], {{0, 0, 1}}); }
}
return combis[total];
}
关于c++ - 输出所有组合的硬币找零算法是否仍然可以通过DP解决?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33340207/
我学过这两种DP方式,现在很迷茫。不同情况下我们如何选择?而且我发现在大多数情况下,自上而下对我来说更自然。谁能告诉我如何做出选择。 PS:这篇文章我看过older post但仍然感到困惑。需要帮忙。
我在想是否有某种通用的方法可以将自上而下的动态编程转换为自下而上的编程。 我们能否想出某种机制来提供正式的方式,通过这种方式我们可以将自上而下的 DP 转换为自下而上的 DP。 注意:我是动态规划的初
在我的 xml 中,我在 TextView 上有这个: android:textSize="18dp"(是的,我知道 dp 不适合文本,但这不是重点) 我还有其他一些以编程方式创建的 TextView
假设我正在为屏幕尺寸等于或大于 600dp 的设备开发不同的布局。我想使用 post android 3.2 资源限定符。我创建了一个名为 layout-sw600dp 的文件夹并将我的布局放在那里,
最近我在 https://leetcode.com/problems/palindrome-partitioning-ii/ 上遇到了这个问题: 给定一个字符串s,划分s使得划分的每个子串都是回文。
我已经尝试使用 admob 大约 4 个小时了,现在真的卡住了。我得到: Not enough space to show ad. Needs 320x50 dp, but only has 288
我正在尝试将可变数量的像素计算为与密度无关的像素,反之亦然。 这个公式 (px to dp): dp = (int)(px/(displayMetrics.densityDpi/160)); 不适用于
我正在从头开始创建一个 HTML 页面,并在其中动态放置值,然后将其显示在 WebView 中。 我正在尝试在 WebView(作为 HTML)中复制 ActionBar(出现在应用程序的其他 Act
我正在使用 the 4.17.37 of Bootstrap 3 Datepicker - eonasdan datepicker 我有一个正确显示的内联日期选择器,我只会使用天模式,所以我使用以下代
我正在尝试将智能横幅广告添加到我的 fragment 布局中,但我不断得到的是“没有足够的空间来展示广告。需要 320x50 dp,但只有 309x0 dp”。 这是我的 fragment 布局。
我在关于 http://developer.android.com/guide/practices/screens_support.html 的一些不幸的歧义中迷失了方向,而且我找不到澄清问题的 st
写在前面 $ DP $,是每个信息学竞赛选手所必会的算法,而 $ DP $ 中状态的转移又显得尤为关键。本文主要从状态的设计和转移入手,利用各种方法对朴素 $ DP $ 的时间复杂度和空间复杂度进行
我试图理解一种著名的正则表达式匹配 DP 算法。以防万一,人们不知道这是描述和算法。 '.' Matches any single character. '*' Matches zero or mor
注意:是的,我知道Android中还有其他处理按钮的方法,但这只是一个示例来说明我的问题(实际的按钮要复杂得多)。因此,请不要在为Android中的按钮提供其他解决方案时回信,我正在寻找PaintCo
Closed. This question needs to be more focused。它当前不接受答案。 想改善这个问题吗?更新问题,使其仅关注editing this post的一个问题。
我正在测试 Samsung Galaxy Note 3根据文档,它是 1080 x 1920 像素(~386 ppi 像素密度)。我想弄清楚 dp 中的屏幕宽度 是多少,以便我可以正确应用并检查 ne
所以我现在完全迷路了,当用户通过Accessibility -> Font size 将字体大小时设置为Huge 时,文本大小会破坏我的 UI,所以为了快速修复 我决定将所有文本大小更改为 dp 而不
更新:我发现了我的问题。我已经为不同的分辨率定义了单独的可绘制资源,但我不知道我必须为不同的屏幕尺寸创建不同的布局。我想我应该更全面地阅读“支持多屏幕”页面。遇到此问题的任何其他人,请阅读整个页面:
阅读这篇文章(http://developer.android.com/guide/practices/screens_support.html)后,我在 xml 文件中使用 dp 单元开发了一个完整
该问题需要计算特定成本的硬币变化次数。 例如,如果我有 50, 20, 10, 5, 1 的硬币值(value),我可以形成以下成本: 5 => (5), (11111), 这是2种方式。 10 =>
我是一名优秀的程序员,十分优秀!