aboutsummaryrefslogtreecommitdiff
path: root/challenge-261/ulrich-rieke/cpp
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2024-03-18 17:15:14 +0000
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2024-03-18 17:15:14 +0000
commitb91a4623cd4c2b51f779f59b53310a0bd745f03f (patch)
tree2c384443a6fd55d2c7e9d09429ba5aefa253e849 /challenge-261/ulrich-rieke/cpp
parent9f566417b406a3632edf6e95be57a9ae4c334703 (diff)
downloadperlweeklychallenge-club-b91a4623cd4c2b51f779f59b53310a0bd745f03f.tar.gz
perlweeklychallenge-club-b91a4623cd4c2b51f779f59b53310a0bd745f03f.tar.bz2
perlweeklychallenge-club-b91a4623cd4c2b51f779f59b53310a0bd745f03f.zip
- Added solutions by Eric Cheung.
- Added solutions by Ulrich Rieke. - Added solutions by Andrew Shitov. - Added solutions by Peter Meszaros. - Added solutions by Feng Chang. - Added solutions by Mark Anderson. - Added solutions by Peter Campbell Smith. - Added solutions by E. Choroba. - Added solutions by W. Luis Mochan. - Added solutions by David Ferrone.
Diffstat (limited to 'challenge-261/ulrich-rieke/cpp')
-rwxr-xr-xchallenge-261/ulrich-rieke/cpp/ch-1.cpp30
-rwxr-xr-xchallenge-261/ulrich-rieke/cpp/ch-2.cpp35
2 files changed, 65 insertions, 0 deletions
diff --git a/challenge-261/ulrich-rieke/cpp/ch-1.cpp b/challenge-261/ulrich-rieke/cpp/ch-1.cpp
new file mode 100755
index 0000000000..a088bfc147
--- /dev/null
+++ b/challenge-261/ulrich-rieke/cpp/ch-1.cpp
@@ -0,0 +1,30 @@
+#include <iostream>
+#include <numeric>
+#include <vector>
+#include <iterator>
+#include <cstdlib>
+
+int digitsum( int n ) {
+ int sum = 0 ;
+ while ( n != 0 ) {
+ sum += n % 10 ;
+ n /= 10 ;
+ }
+ return sum ;
+}
+
+int main( ) {
+ std::cout << "Enter some integers, separated by blanks!\n" ;
+ std::cout << "Enter e to end!\n" ;
+ std::vector<int> numbers {std::istream_iterator<int>{std::cin} ,
+ std::istream_iterator<int>{}} ;
+ int element_sum = std::accumulate( numbers.begin( ) , numbers.end( ) ,
+ 0 ) ;
+ std::vector<int> digitsums ;
+ for ( int i : numbers )
+ digitsums.push_back( digitsum( i ) ) ;
+ int total_digitsums = std::accumulate( digitsums.begin( ) ,
+ digitsums.end( ) , 0 ) ;
+ std::cout << std::abs( total_digitsums - element_sum) << '\n' ;
+ return 0 ;
+}
diff --git a/challenge-261/ulrich-rieke/cpp/ch-2.cpp b/challenge-261/ulrich-rieke/cpp/ch-2.cpp
new file mode 100755
index 0000000000..e5a956a286
--- /dev/null
+++ b/challenge-261/ulrich-rieke/cpp/ch-2.cpp
@@ -0,0 +1,35 @@
+#include <iostream>
+#include <vector>
+#include <algorithm>
+#include <string>
+
+std::vector<std::string> split( const std::string & startline ,
+ const std::string & sep ) {
+ std::vector<std::string> separated ;
+ std::string::size_type start { 0 } ;
+ std::string::size_type pos ;
+ do {
+ pos = startline.find_first_of( sep , start ) ;
+ separated.push_back( startline.substr(start , pos - start )) ;
+ start = pos + 1 ;
+ } while ( pos != std::string::npos ) ;
+ return separated ;
+}
+
+int main( ) {
+ std::cout << "Enter some integers, separated by blanks!\n" ;
+ std::string numberline ;
+ std::getline( std::cin , numberline ) ;
+ std::vector<std::string> innumbers { split( numberline , " " ) } ;
+ std::vector<int> numbers ;
+ for ( auto w : innumbers )
+ numbers.push_back( std::stoi( w ) ) ;
+ std::cout << "Enter a start number!\n" ;
+ int start ;
+ std::cin >> start ;
+ while ( std::find( numbers.begin( ) , numbers.end( ) , start ) !=
+ numbers.end( ) )
+ start *= 2 ;
+ std::cout << start << '\n' ;
+ return 0 ;
+}