
C/C++
Решить билет по c++
Решить билет по программированию c++ оплачу на карту!!! Срочно!

#include<iostream>
#include<fstream>
#include<vector>
#include<map>
using namespace std;
class REG_MARK
{
typedef pair<int, int> pa;
typedef vector<pa> vpa;
typedef map<int, int> mpa;
public: static vpa result;
static bool found(vpa cash, mpa& marks, int max_cup, int ostatok)
{
vpa another_cash;
int another_ostatok{};
auto i = marks.begin();
int nal{};
nal = ostatok / max_cup;
int nex_cup{};
for (; i != marks.end(); i++)
{
if (i->first < max_cup) nex_cup = i->first; else break; //поиск следующего меньшего номинала купюры иначе останется 0
}
if (nal > marks[max_cup]) nal = marks[max_cup]; //если купюр не хватает то ограничить
else
{
another_ostatok = ostatok - (nal - 1) * max_cup; //если хватает добавить второй вариан с -1 купюрой
another_cash = cash;
if (nal-1) another_cash.push_back(pa(max_cup, nal - 1));
}
ostatok -= nal * max_cup;
if (nal) cash.push_back(pa(max_cup, nal));
if (!ostatok)
{
result = cash; return true;
}
return nex_cup && (found(cash, marks, nex_cup, ostatok) || (another_ostatok && found(another_cash, marks, nex_cup, another_ostatok)));
}
public:
static void RangeSum(string PATH, int sum)
{
vpa nachal;
int ostatok = sum;
ifstream file_i(PATH);
int max_cup{};
mpa marks;
int nominal{}, quant{}, bigg_quont{}, my_summ{};
while ((file_i >> nominal) && (file_i >> quant)) marks.insert(pair<int, int>(nominal, quant));
auto i = marks.begin();
for (; i != marks.end(); i++) if (i->first <= sum) max_cup = i->first; // поиск наибольшей купюры менее суммы
if (max_cup && found(nachal, marks, max_cup, sum)) cout << "found!" << endl; else cout << "Невозможно завершить операцию!";
}
};
vector<pair<int, int>> REG_MARK::result{};
int main()
{
setlocale(0, "");
REG_MARK::RangeSum("N:/test.txt", 5000);
for (auto& i : REG_MARK::result) cout << "Номинал " << i.first << " = " << i.second << " штук" << endl;
}
Делать было нехрен... Написал самое сложное - подбор купюр и их выдачу. Дело за малым - в случае успеха вычесть из мапы банкомата вектор результата и вернуть в файл, пустяки ))
PS - вот не понял про какие 20 результатов в test.txt говорится, если туда мы купюры загружаем?
#include<fstream>
#include<vector>
#include<map>
using namespace std;
class REG_MARK
{
typedef pair<int, int> pa;
typedef vector<pa> vpa;
typedef map<int, int> mpa;
public: static vpa result;
static bool found(vpa cash, mpa& marks, int max_cup, int ostatok)
{
vpa another_cash;
int another_ostatok{};
auto i = marks.begin();
int nal{};
nal = ostatok / max_cup;
int nex_cup{};
for (; i != marks.end(); i++)
{
if (i->first < max_cup) nex_cup = i->first; else break; //поиск следующего меньшего номинала купюры иначе останется 0
}
if (nal > marks[max_cup]) nal = marks[max_cup]; //если купюр не хватает то ограничить
else
{
another_ostatok = ostatok - (nal - 1) * max_cup; //если хватает добавить второй вариан с -1 купюрой
another_cash = cash;
if (nal-1) another_cash.push_back(pa(max_cup, nal - 1));
}
ostatok -= nal * max_cup;
if (nal) cash.push_back(pa(max_cup, nal));
if (!ostatok)
{
result = cash; return true;
}
return nex_cup && (found(cash, marks, nex_cup, ostatok) || (another_ostatok && found(another_cash, marks, nex_cup, another_ostatok)));
}
public:
static void RangeSum(string PATH, int sum)
{
vpa nachal;
int ostatok = sum;
ifstream file_i(PATH);
int max_cup{};
mpa marks;
int nominal{}, quant{}, bigg_quont{}, my_summ{};
while ((file_i >> nominal) && (file_i >> quant)) marks.insert(pair<int, int>(nominal, quant));
auto i = marks.begin();
for (; i != marks.end(); i++) if (i->first <= sum) max_cup = i->first; // поиск наибольшей купюры менее суммы
if (max_cup && found(nachal, marks, max_cup, sum)) cout << "found!" << endl; else cout << "Невозможно завершить операцию!";
}
};
vector<pair<int, int>> REG_MARK::result{};
int main()
{
setlocale(0, "");
REG_MARK::RangeSum("N:/test.txt", 5000);
for (auto& i : REG_MARK::result) cout << "Номинал " << i.first << " = " << i.second << " штук" << endl;
}
Делать было нехрен... Написал самое сложное - подбор купюр и их выдачу. Дело за малым - в случае успеха вычесть из мапы банкомата вектор результата и вернуть в файл, пустяки ))
PS - вот не понял про какие 20 результатов в test.txt говорится, если туда мы купюры загружаем?
Похожие вопросы
- Помогите решить задачу по C++!
- Помогите решить задание на C++
- Нужно срочно решить задачу на C++
- Помогите решить задачу на C++
- Помогите решить задание на c++
- Помогите решить задачу на C++.
- Помогите пожалуйста решить задачу на C ИЛИ C#
- Помогите решить задачу на c++
- Решите задачу на C++
- СРОЧНО! Помогите решить 2 задачи C++
if (max_cup && found(nachal, marks, max_cup, sum))
{
for (auto& i : result)
{
marks[i.first] -= i.second; //удаление выданных банкнот
ofstream file_o(PATH);
for (auto& var : marks)
{
if (var.second) file_o << var.first << " " << var.second << '\n';
}
file_o.close();
}
cout << "Спасибо что пользуетесь нашим банком!\n";
}
else cout << "Извините, операция невозможна.\n";