diff --git a/Katas/Algorithms/greedy/Greedy.md b/Katas/Algorithms/greedy/Greedy.md new file mode 100644 index 0000000..8de2ffd --- /dev/null +++ b/Katas/Algorithms/greedy/Greedy.md @@ -0,0 +1,13 @@ +# Greedy + +Greedy is an algorithmic paradigm that builds a problem piece by piece, choosing in that moment the next piece that seems more efficient. + +Don't use the divide and conquer; we go right away to the answer, and we make the best decision at the moment. Regardless of the future, don't be the best. + +###### _Example:_ + +- [Coin Example](./example/src/solution.cpp) + +###### **Katas:** + +- [2276A Perfect Sandwiches](../../CP/perfect_sanduches/src/solution.cpp) diff --git a/Katas/Algorithms/greedy/example/src/solution.cpp b/Katas/Algorithms/greedy/example/src/solution.cpp new file mode 100644 index 0000000..17d90b5 --- /dev/null +++ b/Katas/Algorithms/greedy/example/src/solution.cpp @@ -0,0 +1,28 @@ +#include +int main() { + + int n; + int amount; + std::cin >> n >> amount; + int coins[n]; + int rest = 0; + for (int i = 0; i < n; i++) { + std::cin >> coins[i]; + } + + for (int i = n - 1; i >= 0; i--) { + if (amount >= coins[i]) { + int cnt = (amount / coins[i]); + rest += cnt; + amount -= (cnt * coins[i]); + } + + if (amount == 0) { + break; + } + } + + std::cout << rest << std::endl; + + return 0; +} diff --git a/Katas/Algorithms/greedy/example/test/expected1.txt b/Katas/Algorithms/greedy/example/test/expected1.txt new file mode 100644 index 0000000..00750ed --- /dev/null +++ b/Katas/Algorithms/greedy/example/test/expected1.txt @@ -0,0 +1 @@ +3 diff --git a/Katas/Algorithms/greedy/example/test/input1.txt b/Katas/Algorithms/greedy/example/test/input1.txt new file mode 100644 index 0000000..bb54689 --- /dev/null +++ b/Katas/Algorithms/greedy/example/test/input1.txt @@ -0,0 +1,2 @@ +3 20 +1 10 18 diff --git a/Katas/CP/nexround/src/solution.cpp b/Katas/CP/nexround/src/solution.cpp new file mode 100644 index 0000000..e3e6d39 --- /dev/null +++ b/Katas/CP/nexround/src/solution.cpp @@ -0,0 +1,35 @@ +#include +#include +#include +int main() { + std::ios_base::sync_with_stdio(false); + std::cin.tie(NULL); + + int n, k; + std::cin >> n >> k; + int scores[n]; + + for (int i = 0; i < n; i++) { + std::cin >> scores[i]; + } + + int count = 0; + for (int i = 0; i < n; i++) { + if (scores[i] == 0) + break; + else { + if (scores[i] > 0 && i + 1 <= k) { + count++; + } else { + if (scores[i] == scores[k - 1]) { + count++; + } else { + break; + } + } + } + } + std::cout << count << std::endl; + + return 0; +} diff --git a/Katas/CP/nexround/test/expected1.txt b/Katas/CP/nexround/test/expected1.txt new file mode 100644 index 0000000..1e8b314 --- /dev/null +++ b/Katas/CP/nexround/test/expected1.txt @@ -0,0 +1 @@ +6 diff --git a/Katas/CP/nexround/test/input1.txt b/Katas/CP/nexround/test/input1.txt new file mode 100644 index 0000000..ed179c9 --- /dev/null +++ b/Katas/CP/nexround/test/input1.txt @@ -0,0 +1,2 @@ +8 5 +10 9 8 7 7 7 5 5 diff --git a/Katas/CP/sauSaGebank/src/solution.cpp b/Katas/CP/sauSaGebank/src/solution.cpp new file mode 100644 index 0000000..8e56555 --- /dev/null +++ b/Katas/CP/sauSaGebank/src/solution.cpp @@ -0,0 +1,42 @@ +// #include +// #include +// int main() { +// int t, n, k; +// int a = 1; +// int c = 0; +// +// std::cin >> t; +// for (int i = 0; i < t; i++) { +// std::cin >> n >> k; +// for (int j = n; j > 0; j--) { +// if (j >= k) { +// if (j == (n - k)) { +// c += a; +// a = 1; +// k--; +// } else { +// a = pow(2, j); +// } +// } +// } +// std::cout << c << std::endl; +// } +// +// return 0; +// } + +#include +using namespace std; +int main() { + cin.tie(0); + cout.tie(0); + ios_base::sync_with_stdio(0); + int tc; + cin >> tc; + while (tc--) { + int n, k; + cin >> n >> k; + cout << (1 << (n - k + 1)) + 2 * (k - 1) << '\n'; + } + return 0; +} diff --git a/Katas/CP/sauSaGebank/src/solutionv2 b/Katas/CP/sauSaGebank/src/solutionv2 new file mode 100755 index 0000000..d87bdc2 Binary files /dev/null and b/Katas/CP/sauSaGebank/src/solutionv2 differ diff --git a/Katas/CP/sauSaGebank/test/expected1.txt b/Katas/CP/sauSaGebank/test/expected1.txt new file mode 100644 index 0000000..884dbe8 --- /dev/null +++ b/Katas/CP/sauSaGebank/test/expected1.txt @@ -0,0 +1,5 @@ +2 +4 +8 +10 +514 diff --git a/Katas/CP/sauSaGebank/test/input1.txt b/Katas/CP/sauSaGebank/test/input1.txt new file mode 100644 index 0000000..285ea26 --- /dev/null +++ b/Katas/CP/sauSaGebank/test/input1.txt @@ -0,0 +1,6 @@ +5 +1 1 +2 1 +4 3 +5 5 +10 2 diff --git a/Katas/README.md b/Katas/README.md index 138c8c0..c16ee59 100644 --- a/Katas/README.md +++ b/Katas/README.md @@ -1 +1,7 @@ -# 2 codeforces and contest +# Code forces + +I share my logic and my mistakes + +### TOPICS + +- [Greedy](./Algorithms/greedy/Greedy.md)