diff options
Diffstat (limited to 'challenge-238/deadmarshal/cpp')
| -rw-r--r-- | challenge-238/deadmarshal/cpp/ch-1.cpp | 23 | ||||
| -rw-r--r-- | challenge-238/deadmarshal/cpp/ch-2.cpp | 55 |
2 files changed, 78 insertions, 0 deletions
diff --git a/challenge-238/deadmarshal/cpp/ch-1.cpp b/challenge-238/deadmarshal/cpp/ch-1.cpp new file mode 100644 index 0000000000..9d2ee4ab6d --- /dev/null +++ b/challenge-238/deadmarshal/cpp/ch-1.cpp @@ -0,0 +1,23 @@ +#include<iostream> +#include<vector> +#include<numeric> +#include<iterator> + +template<typename T> +void running_sum(const std::vector<T> &vec) +{ + std::vector<T> ret{}; + std::partial_sum(vec.cbegin(),vec.cend(), + std::ostream_iterator<T>(std::cout, " ")); + std::cout << '\n'; +} + +int main(void) +{ + std::vector<int> vec1{1,2,3,4,5},vec2{1,1,1,1,1},vec3{0,-1,1,2}; + running_sum<int>(vec1); + running_sum<int>(vec2); + running_sum<int>(vec3); + return 0; +} + diff --git a/challenge-238/deadmarshal/cpp/ch-2.cpp b/challenge-238/deadmarshal/cpp/ch-2.cpp new file mode 100644 index 0000000000..e2c00022aa --- /dev/null +++ b/challenge-238/deadmarshal/cpp/ch-2.cpp @@ -0,0 +1,55 @@ +#include<iostream> +#include<vector> +#include<algorithm> + +template<typename T> +T product(T n) +{ + T prod = 1; + while(n) + { + prod *= n % 10; + n /= 10; + } + return prod; +} + +template<typename T> +T helper(T n) +{ + T sum = 0; + while(n >= 10) + { + sum++; + n = product<T>(n); + } + return sum; +} + +template<typename T> +void persistence_sort(std::vector<T>& v) +{ + std::sort(v.begin(),v.end(),[](T a, T b){ + T ha = helper<T>(a), hb = helper<T>(b); + return ha == hb ? a < b : ha < hb; + }); +} + +template<typename T> +std::ostream &operator<<(std::ostream &os, + const std::vector<T>& vec) +{ + for(const auto &e : vec) os << e << ' '; + os << "\n"; + return os; +} + +int main() +{ + std::vector<int> vec1{15,99,1,34},vec2{50,25,33,22}; + persistence_sort(vec1); + persistence_sort(vec2); + std::cout << vec1 << vec2; + return 0; +} + |
