diff options
| author | Mohammad Sajid Anwar <mohammad.anwar@yahoo.com> | 2025-02-03 17:29:40 +0000 |
|---|---|---|
| committer | Mohammad Sajid Anwar <mohammad.anwar@yahoo.com> | 2025-02-03 17:29:40 +0000 |
| commit | aaab417272f7ae13ade34c68a033d2b1214886d3 (patch) | |
| tree | faa6dbd23c6f83d75adb7d17ae31402afcb1dc8e /challenge-307/ulrich-rieke/cpp/ch-1.cpp | |
| parent | 22771160a2fd5e5893342b5f28ee039872842852 (diff) | |
| download | perlweeklychallenge-club-aaab417272f7ae13ade34c68a033d2b1214886d3.tar.gz perlweeklychallenge-club-aaab417272f7ae13ade34c68a033d2b1214886d3.tar.bz2 perlweeklychallenge-club-aaab417272f7ae13ade34c68a033d2b1214886d3.zip | |
- Added solutions by Ulrich Rieke.
Diffstat (limited to 'challenge-307/ulrich-rieke/cpp/ch-1.cpp')
| -rwxr-xr-x | challenge-307/ulrich-rieke/cpp/ch-1.cpp | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/challenge-307/ulrich-rieke/cpp/ch-1.cpp b/challenge-307/ulrich-rieke/cpp/ch-1.cpp new file mode 100755 index 0000000000..df6d096722 --- /dev/null +++ b/challenge-307/ulrich-rieke/cpp/ch-1.cpp @@ -0,0 +1,45 @@ +#include <string>
+#include <vector>
+#include <iostream>
+#include <sstream>
+#include <utility>
+#include <algorithm>
+
+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 main( ) {
+ std::cout << "Enter some integers separated by whitespace!\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 ) ) ;
+ std::vector<std::pair<int , int>> before_sort , after_sort ;
+ int len = numbers.size( ) ;
+ for ( int i = 0 ; i < len ; i++ ) {
+ before_sort.push_back( std::make_pair( i , numbers[i] ) ) ;
+ }
+ std::sort( numbers.begin( ) , numbers.end( ) ) ;
+ for ( int i = 0 ; i < len ; i++ ) {
+ after_sort.push_back( std::make_pair( i , numbers[i] ) ) ;
+ }
+ std::vector<int> changed_indices ;
+ for ( int i = 0 ; i < len ; i++ ) {
+ if ( (before_sort.begin( ) + i)->second != (after_sort.begin( ) + i)->second )
+ changed_indices.push_back( (before_sort.begin( ) + i)->first ) ;
+ }
+ std::cout << "( " ;
+ for ( int i : changed_indices )
+ std::cout << i << ' ' ;
+ std::cout << ")\n" ;
+ return 0 ;
+}
|
