aboutsummaryrefslogtreecommitdiff
path: root/challenge-283/ulrich-rieke/cpp/ch-2.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-283/ulrich-rieke/cpp/ch-2.cpp')
-rwxr-xr-xchallenge-283/ulrich-rieke/cpp/ch-2.cpp35
1 files changed, 35 insertions, 0 deletions
diff --git a/challenge-283/ulrich-rieke/cpp/ch-2.cpp b/challenge-283/ulrich-rieke/cpp/ch-2.cpp
new file mode 100755
index 0000000000..a53ddde486
--- /dev/null
+++ b/challenge-283/ulrich-rieke/cpp/ch-2.cpp
@@ -0,0 +1,35 @@
+#include <vector>
+#include <sstream>
+#include <string>
+#include <algorithm>
+#include <iostream>
+#include <numeric>
+
+std::vector<std::string> split( std::string text , const char delimiter ) {
+ std::stringstream instr { text } ;
+ std::vector<std::string> allTokens ;
+ std::string word ;
+ while ( std::getline( instr, word , delimiter ) ) {
+ allTokens.push_back( word ) ;
+ }
+ return allTokens ;
+}
+
+int main( ) {
+ std::cout << "Enter some positive integers, separated by blanks!\n" ;
+ std::string line ;
+ std::getline( std::cin , line ) ;
+ std::vector<std::string> tokens { split( line, ' ' ) } ;
+ std::vector<int> numbers ;
+ for ( auto s : tokens )
+ numbers.push_back( std::stoi( s ) ) ;
+ int len = numbers.size( ) ;
+ std::vector<int> indices( len ) ;
+ std::iota( indices.begin( ) , indices.end( ) , 0 ) ;
+ bool result = std::all_of( indices.begin( ) , indices.end( ) ,
+ [&numbers]( int i ) { return std::count( numbers.begin( ) ,
+ numbers.end( ) , i ) == numbers[ i ] ; } ) ;
+ std::cout << std::boolalpha << result << '\n' ;
+ return 0 ;
+}
+