diff options
| author | deadmarshal <adeadmarshal@gmail.com> | 2023-01-28 20:44:13 +0330 |
|---|---|---|
| committer | deadmarshal <adeadmarshal@gmail.com> | 2023-01-28 20:44:13 +0330 |
| commit | 79df3f502658e9e7622ce49faa32ee703d936f67 (patch) | |
| tree | fc3a7c9a5c0f8ef69b77122b254f62e3578effcf /challenge-201/deadmarshal/cpp | |
| parent | 27b88f614b9bb53872ef0da19a56087505836db0 (diff) | |
| download | perlweeklychallenge-club-79df3f502658e9e7622ce49faa32ee703d936f67.tar.gz perlweeklychallenge-club-79df3f502658e9e7622ce49faa32ee703d936f67.tar.bz2 perlweeklychallenge-club-79df3f502658e9e7622ce49faa32ee703d936f67.zip | |
TWC201
Diffstat (limited to 'challenge-201/deadmarshal/cpp')
| -rw-r--r-- | challenge-201/deadmarshal/cpp/ch-1.cpp | 22 | ||||
| -rw-r--r-- | challenge-201/deadmarshal/cpp/ch-2.cpp | 31 |
2 files changed, 53 insertions, 0 deletions
diff --git a/challenge-201/deadmarshal/cpp/ch-1.cpp b/challenge-201/deadmarshal/cpp/ch-1.cpp new file mode 100644 index 0000000000..c1b8a37d4f --- /dev/null +++ b/challenge-201/deadmarshal/cpp/ch-1.cpp @@ -0,0 +1,22 @@ +#include<iostream> +#include<vector> +#include<unordered_map> + +template<typename T> +void missing_numbers(const std::vector<T> &vec) +{ + std::unordered_map<T,T> m{}; + for(size_t i = 0; i < vec.size(); ++i) m[vec[i]] = 1; + for(size_t i = 0; i < vec.size()+1; ++i) + if(m[i] != 1) std::cout << i << ' '; + std::cout << '\n'; +} + +int main() +{ + const std::vector<int> vec1{0,1,3},vec2{0,1}; + missing_numbers<int>(vec1); + missing_numbers<int>(vec2); + return 0; +} + diff --git a/challenge-201/deadmarshal/cpp/ch-2.cpp b/challenge-201/deadmarshal/cpp/ch-2.cpp new file mode 100644 index 0000000000..d05bc157e5 --- /dev/null +++ b/challenge-201/deadmarshal/cpp/ch-2.cpp @@ -0,0 +1,31 @@ +#include<iostream> + +#define N 5 +static int count{}; + +void find_combinations(int *arr, + int index, + int num, + int reduced_num) +{ + if(reduced_num < 0) return; + if(reduced_num == 0) + { + count++; + return; + } + int prev = index == 0 ? 1 : arr[index-1]; + for(int i = prev; i <= num; ++i) + { + arr[index] = i; + find_combinations(arr,index+1,num,reduced_num-i); + } +} + +int main() +{ + int arr[N]; + find_combinations(arr,0,N,N); + std::cout << count << '\n'; +} + |
