diff options
author | Santo Cariotti <santo@dcariotti.me> | 2023-01-22 21:01:09 +0100 |
---|---|---|
committer | Santo Cariotti <santo@dcariotti.me> | 2023-01-22 21:01:09 +0100 |
commit | 310f312a04cf292a4b29d45b1bdb13077d9a1cf7 (patch) | |
tree | bf4a27db327e9904eadd3967586c8d68dd870bf9 /Year_2 | |
parent | 9e241889fa477f05983d2b6542e183387ef27e47 (diff) |
adds
Diffstat (limited to 'Year_2')
-rw-r--r-- | Year_2/Algorithms/resto.cc | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/Year_2/Algorithms/resto.cc b/Year_2/Algorithms/resto.cc new file mode 100644 index 0000000..53fd6f6 --- /dev/null +++ b/Year_2/Algorithms/resto.cc @@ -0,0 +1,54 @@ +#include <iostream> +#include <vector> +#include <fstream> +#include <cstring> +#define INF 9999999 + +using namespace std; + +int +change(vector<int>& coins, int sum) +{ + int n = coins.size(); + + int* table = new int[sum+1]; + memset(table, sum, 0); + + table[0] = 0; + for (int i = 1; i < sum+1; ++i) { + table[i] = INF; + for (int j = 0; j < n; ++j) { + if (coins[j] <= i) + table[i] = min(table[i], 1+table[i-coins[j]]); + } + } + + int result = table[sum]; + delete[] table; + + return result; +} + +int +main(int argc, char** argv) +{ + int ts = (argc > 1) ? stoi(argv[1]) : 100; + int s, n; + ifstream fin("input.txt"); + ofstream fout("output.txt"); + + for (int i = 0; i < ts; ++i) { + fin >> s >> n; + vector<int> v(n); + for (int j = 0; j < n; ++j) { + fin >> v[j]; + } + + fout << change(v, s) << endl; + } + + + fin.close(); + fout.close(); + return 0; +} |