aboutsummaryrefslogtreecommitdiff
path: root/challenge-320/ulrich-rieke/cpp/ch-2.cpp
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <mohammad.anwar@yahoo.com>2025-05-06 15:45:31 +0100
committerMohammad Sajid Anwar <mohammad.anwar@yahoo.com>2025-05-06 15:45:31 +0100
commit7d67833e2e7de682d2d418145812d575218e0fa9 (patch)
tree5941416b346b211dfe559083da3d92a708dd4a29 /challenge-320/ulrich-rieke/cpp/ch-2.cpp
parentfd1e9787a80a72ee53be0a313a27c56ecd9d62ee (diff)
downloadperlweeklychallenge-club-7d67833e2e7de682d2d418145812d575218e0fa9.tar.gz
perlweeklychallenge-club-7d67833e2e7de682d2d418145812d575218e0fa9.tar.bz2
perlweeklychallenge-club-7d67833e2e7de682d2d418145812d575218e0fa9.zip
- Added solutions by Eric Cheung.
- Added solutions by Ulrich Rieke. - Added solutions by David Ferrone. - Added solutions by E. Choroba. - Added solutions by Mark Anderson. - Added solutions by Jaldhar H. Vyas. - Added solutions by Ali Moradi. - Added solutions by Feng Chang. - Added solutions by Vinod Kumar K. - Added solutions by Peter Campbell Smith. - Added solutions by Ali Moradi. - Added solutions by Feng Chang. - Added solutions by Conor Hoekstra. - Added solutions by Bob Lied. - Added solutions by Athanasius. - Added solutions by W. Luis Mochan. - Added solutions by Andrew Shitov. - Added solutions by PokGoPun. - Added solutions by Roger Bell_West.
Diffstat (limited to 'challenge-320/ulrich-rieke/cpp/ch-2.cpp')
-rwxr-xr-xchallenge-320/ulrich-rieke/cpp/ch-2.cpp44
1 files changed, 44 insertions, 0 deletions
diff --git a/challenge-320/ulrich-rieke/cpp/ch-2.cpp b/challenge-320/ulrich-rieke/cpp/ch-2.cpp
new file mode 100755
index 0000000000..9950e4e891
--- /dev/null
+++ b/challenge-320/ulrich-rieke/cpp/ch-2.cpp
@@ -0,0 +1,44 @@
+#include <iostream>
+#include <sstream>
+#include <numeric>
+#include <string>
+#include <vector>
+#include <cstdlib>
+
+std::vector<std::string> split( const std::string & text , char delimiter ) {
+ std::vector<std::string> tokens ;
+ std::istringstream istr { text } ;
+ std::string word ;
+ while ( std::getline( istr , word , delimiter ) )
+ tokens.push_back( word ) ;
+ return tokens ;
+}
+
+int digitsum( int number ) {
+ int sum = 0 ;
+ while ( number != 0 ) {
+ sum += number % 10 ;
+ number /= 10 ;
+ }
+ return sum ;
+}
+
+int main( ) {
+ std::cout << "Enter some positive integers separated by blanks!\n" ;
+ std::string line ;
+ std::getline( std::cin , line ) ;
+ auto tokens { split( line , ' ' ) } ;
+ std::vector<int> numbers ;
+ for ( auto s : tokens )
+ numbers.push_back( std::stoi( s ) ) ;
+ int numbersum = std::accumulate( numbers.begin( ) , numbers.end( ) , 0 ) ;
+ std::vector<int> digitsums ;
+ for ( int n : numbers ) {
+ digitsums.push_back( digitsum( n ) ) ;
+ }
+ int all_digit_sum = std::accumulate( digitsums.begin( ) , digitsums.end( ) ,
+ 0 ) ;
+ std::cout << std::abs( all_digit_sum - numbersum ) << '\n' ;
+ return 0 ;
+}
+