Это задача о рюкзаке, решаемая с помощью мемоизации и динамического программирования. Проблема в том, что распечатка моих ответов не соответствует требованиям.
Что я сделал не так?
Это файл ProfitHeader.h:
#include
#include
#include
#include
#include "ProfitHeader.h"
using namespace std;
using namespace kproblem;
//The following was provided to us on the program
class IO_Exception : public runtime_error
{
public:
IO_Exception(const string & message) : runtime_error(message) { }
};
void readProjects(vector
& projects, const string & fileName)
{
ifstream infile(fileName.c_str());
if (!infile)
throw IO_Exception("Could not open " + fileName);
string oneLine;
unsigned int lineNum = 0;
while (getline(infile, oneLine))
{
istringstream st(oneLine);
lineNum++;
string name;
Money liquidity;
Labor officeWork;
Labor programmingWork;
Money profit;
st >> name;
st >> liquidity;
st >> officeWork;
st >> programmingWork;
st >> profit;
if (st.fail())
{
cerr
Подробнее здесь: [url]https://stackoverflow.com/questions/5839115/what-did-i-do-wrong-in-this-knapsack-solution-using-dynamic-programming[/url]
Это задача о рюкзаке, решаемая с помощью мемоизации и динамического программирования. Проблема в том, что распечатка моих ответов не соответствует требованиям. Что я сделал не так? Это файл ProfitHeader.h: [code]#ifndef PROFITHEADER_H_ #define PROFITHEADER_H_
#include #include #include using namespace std;
namespace kproblem{ typedef int Money; typedef int Labor;
struct Resources{ Money liquidity; Labor officeWork; Labor programmingWork; Resources(Money li, Labor of, Labor pro) : liquidity(li), officeWork(of), programmingWork(pro){} //operator -= Resources & operator -=( const Resources &rhs ){ liquidity -=rhs.liquidity; officeWork -=rhs.officeWork; programmingWork -=rhs.programmingWork; return *this; } //operator< Used to make sure that key elements Match. will not modify (this) bool operatorliquidity < rhs.liquidity) return true; else if(this->liquidity > rhs.liquidity) return false; else if(this->officeWork < rhs.officeWork) return true; else if(this->officeWork > rhs.officeWork) return false; //this is the iff conditional else if(this->programmingWork < rhs.programmingWork) return true; else return false; } }; //Global Operator-. This will not modify (this). Resources operator-( const Resources & lhs, const Resources & rhs ){ return Resources(lhs.liquidity - rhs.liquidity, lhs.officeWork - rhs.officeWork, lhs.programmingWork - rhs.programmingWork); } //This is the Project Struct. It should contain the resources and data from the file. struct Project{ string name; Resources resources; Money profit; Project(string n, Resources re, Money p) : name(n), resources(re), profit(p) {} }; //Definition of the ValueMap typedef map ::size_type>, pair> ValueMap; } #endif [/code] Это мой файл main.cpp: [code]#include #include #include #include #include "ProfitHeader.h"
using namespace std; using namespace kproblem;
//The following was provided to us on the program class IO_Exception : public runtime_error { public: IO_Exception(const string & message) : runtime_error(message) { } };
void readProjects(vector & projects, const string & fileName) { ifstream infile(fileName.c_str()); if (!infile) throw IO_Exception("Could not open " + fileName); string oneLine; unsigned int lineNum = 0; while (getline(infile, oneLine)) { istringstream st(oneLine); lineNum++; string name; Money liquidity; Labor officeWork; Labor programmingWork; Money profit; st >> name; st >> liquidity; st >> officeWork; st >> programmingWork; st >> profit; if (st.fail()) { cerr
I'm currently working on a knapsack problem and have implemented both a recursive solution without memoization and a memoized version. Surprisingly, the recursive solution is giving me the correct answer, but when I try to memoize the solution, I'm...
Я пытаюсь решить задачу о рюкзаке с двумерным массивом на CodeBlocks, но после запуска программы я нажимаю цифру на клавиатуре, а работающий терминал не отображает нажатое мной число. (Означает ли это, что я ввёл не удачно?) Нажимаю цифру на...
Я использую рюкзак Laravel, и у меня есть таблица с множеством столбцов, таких как 10 или более. Crud :: setFromDB (); В защищенной функции setuplistoperation () моего контроллера таблицы, а также попытался добавить столбцы вручную, используя Crud...
Я использую рюкзак Laravel, и у меня есть таблица с множеством столбцов, таких как 10 или более. Crud :: setFromDB (); В защищенной функции setuplistoperation () моего контроллера таблицы, а также попытался добавить столбцы вручную, используя Crud...
Из кода здесь, $ usercount возвращает не определен в лезвии.
Widget::add([
'type' => 'view',
'view' => 'your.custom.view', // path to your Blade file
'data' => ,
])->to('before_content');