- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想在杆 ct 问题中获得最大 yield 的杆尺寸。假设有 4 根杆。
1 = 5
2 = 4
3 = 3
4 = 10
在这种情况下,最大收入将由 1,1,1,1 产生,即 20。我获得了最大的收入,但我如何获得杆的尺寸这是代码。
#include<iostream>
using namespace std;
int Rod_Cut(int *,int);
int main()
{
const int n=5;
int arr[n]={0,5,4,3,10};
int size[n]={0};
cout<<Rod_Cut(arr,n)<<endl;
}
int Rod_Cut(int *arr, int n)
{
if(n==0)
{
return 0;
}
int q=0;
for(int i=1;i<n;i++)
{
q=max(q,arr[i]+Rod_Cut(arr,n-i));
}
return q;
}
最佳答案
这是一种方法:
您需要返回每次计算中使用的序列。如果新值比您目前拥有的更好,则必须保存新的“最佳”序列。最后,您返回找到的最佳序列。
该提案不是最优的(例如性能),但也许它会给您一些想法。然后您可以优化代码以获得更好的性能。
祝你好运。
int Rod_Cut(int *,int, vector<int>&);
int main()
{
const int n=5;
int arr[n]={0,5,8,3,10};
int size[n]={0};
vector<int> v; // Create vector for the best sequence
cout<<Rod_Cut(arr,n,v)<<endl;
// Print the best sequence
for(auto e : v)
{
cout << e << " ";
}
}
int Rod_Cut(int *arr, int n, vector<int>& v)
{
if(n==0)
{
return 0;
}
int q=0;
vector<int> t; // Temp vector for next call
vector<int> st; // The best sequence found at this level
for(int i=1;i<n;i++)
{
int temp = arr[i]+Rod_Cut(arr,n-i,t);
if (temp > q)
{
// A new better sequence has been found
st.clear(); // clear the former best sequence
for(auto e : t) // add the new best sequence
{
st.push_back(e);
}
st.push_back(i); // add i used at this level
q = temp;
}
t.clear(); // Prepare for next call
}
// Copy the best sequence to the returned vector
for (auto e : st)
{
v.push_back(e);
}
return q;
}
关于c++ - 我如何使用递归获取 Rod Cut 中的 Rod 尺寸?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30420823/
我想在杆 ct 问题中获得最大 yield 的杆尺寸。假设有 4 根杆。 1 = 5 2 = 4 3 = 3 4 = 10 在这种情况下,最大收入将由 1,1,1,1 产生,即 20。我获得了最大的收
我正在关注 isntructions用于将 Nim(rod) 安装到 linux 上。然后我关注了this显示这样做的网站: $ echo 'export PATH=$PATH:$your_insta
问题是: 给定一个数组,表示 table 上杆的长度。每次选择两根杆并将它们连接起来并获得一根新杆。这样做直到 table 上只有一根杆。连接的成本是两根杆的长度之和,例如,连接 2 和 3 将得到
好吧,这可能是个棘手的问题。它实际上是与我的实际应用程序相关的另一个类似问题的类比,但为了清楚起见,我将其简化为这个假设问题。开始了: 我有一排鱼竿需要分类。因为是一条线,所以只需要关注1个维度。 杆
如何修改 cutRod 和 BottomUpCutRod 方法以保存大于数组长度的长度。例如,当前 p 的长度为 11,我如何切割长度为 15、20 等的杆,并具有相同的数组。例如 p = {0, 1
给定一根长度为 n 英寸的杆和一张价格为 pi 的表格i = 1, 2, ... n, 求切分可获得的最大 yield rn杆和卖件。 Bottom_Up_Cut_Rod(p, n) 1 let r[
这是经典的杆切割问题的代码。正如代码所示,大小为 1、2、3 和 4,即价格数组 arr[] 的大小。我将如何修改代码,以便将大小设置为与给定值不同的值。例如,改为 1、2、3 和 5。 stati
所以我在 python 中有这段代码,目前它只返回切割杆的最大值。我怎样才能修改它以让我也知道切割的位置?它采用一个价格列表,其指数 +1 对应于每个长度的杆的值(value),n 对应于杆的长度。
我一直在尝试使用 4.2.6 内核在 archlinux 下编译 Rigs of Rods。我遵循了 http://www.rigsofrods.com/wiki/pages/Compiling_So
我是一名优秀的程序员,十分优秀!