From aaab417272f7ae13ade34c68a033d2b1214886d3 Mon Sep 17 00:00:00 2001 From: Mohammad Sajid Anwar Date: Mon, 3 Feb 2025 17:29:40 +0000 Subject: - Added solutions by Ulrich Rieke. --- challenge-307/ulrich-rieke/cpp/ch-1.cpp | 45 +++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100755 challenge-307/ulrich-rieke/cpp/ch-1.cpp (limited to 'challenge-307/ulrich-rieke/cpp/ch-1.cpp') 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 +#include +#include +#include +#include +#include + +std::vector split( const std::string & text , char delimiter ) { + std::vector 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 numbers ; + for ( auto s : tokens ) + numbers.push_back( std::stoi( s ) ) ; + std::vector> 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 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 ; +} -- cgit