aboutsummaryrefslogtreecommitdiff
path: root/challenge-241/deadmarshal/cpp/ch-2.cpp
diff options
context:
space:
mode:
author冯昶 <fengchang@novel-supertv.com>2023-11-13 10:15:17 +0800
committer冯昶 <fengchang@novel-supertv.com>2023-11-13 10:15:17 +0800
commit41f99d3c168e8def8c2a799c592282acf0d275a8 (patch)
treee862f32c73ecc3b39546d8f2e40268097bdc84eb /challenge-241/deadmarshal/cpp/ch-2.cpp
parent9831ad5b94643aec63e30e720b83dff7a5eac18b (diff)
parentf4d46d9aa21b95dbb99eec92f338d157273fbbdb (diff)
downloadperlweeklychallenge-club-41f99d3c168e8def8c2a799c592282acf0d275a8.tar.gz
perlweeklychallenge-club-41f99d3c168e8def8c2a799c592282acf0d275a8.tar.bz2
perlweeklychallenge-club-41f99d3c168e8def8c2a799c592282acf0d275a8.zip
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-241/deadmarshal/cpp/ch-2.cpp')
-rw-r--r--challenge-241/deadmarshal/cpp/ch-2.cpp48
1 files changed, 48 insertions, 0 deletions
diff --git a/challenge-241/deadmarshal/cpp/ch-2.cpp b/challenge-241/deadmarshal/cpp/ch-2.cpp
new file mode 100644
index 0000000000..b53ab789cb
--- /dev/null
+++ b/challenge-241/deadmarshal/cpp/ch-2.cpp
@@ -0,0 +1,48 @@
+#include<iostream>
+#include<vector>
+#include<algorithm>
+
+template<typename T>
+size_t count_factors(T n)
+{
+ size_t count{};
+ T c = 2;
+ while(n > 1)
+ {
+ if(n % c == 0)
+ {
+ n /= c;
+ count++;
+ }
+ else c++;
+ }
+ return count;
+}
+
+template<typename T>
+void prime_order(std::vector<T> &vec)
+{
+ std::sort(vec.begin(),vec.end(),[&](T a, T b){
+ size_t fa = count_factors<T>(a);
+ size_t fb = count_factors<T>(b);
+ return fa == fb ? a < b : fa < fb;
+ });
+}
+
+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> vec{11,8,27,4};
+ prime_order<int>(vec);
+ std::cout << vec;
+ return 0;
+}
+