From e9d030e5fd8fa107797fac1489bca6ce2724d212 Mon Sep 17 00:00:00 2001 From: Mohammad Sajid Anwar Date: Mon, 10 Feb 2025 20:39:47 +0000 Subject: - Added solutions by Eric Cheung. - Added solutions by Ulrich Rieke. - Added solutions by Mark Anderson. - Added solutions by David Ferrone. - Added solutions by Ali Moradi. - Added solutions by Niels van Dijke. - Added solutions by Peter Pentchev. - Added solutions by E. Choroba. - Added solutions by Peter Meszaros. - Added solutions by Thomas Kohler. - Added solutions by W. Luis Mochan. - Added solutions by BarrOff. --- challenge-308/ulrich-rieke/cpp/ch-2.cpp | 42 +++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100755 challenge-308/ulrich-rieke/cpp/ch-2.cpp (limited to 'challenge-308/ulrich-rieke/cpp/ch-2.cpp') diff --git a/challenge-308/ulrich-rieke/cpp/ch-2.cpp b/challenge-308/ulrich-rieke/cpp/ch-2.cpp new file mode 100755 index 0000000000..8a80cf6fc5 --- /dev/null +++ b/challenge-308/ulrich-rieke/cpp/ch-2.cpp @@ -0,0 +1,42 @@ +#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 numbers separated by whitespace!\n" ; + std::string line ; + std::getline( std::cin , line ) ; + auto tokens { split( line , ' ' ) } ; + std::vector encoded ; + for ( auto s : tokens ) + encoded.push_back( std::stoi( s ) ) ; + std::cout << "Enter an initial integer!\n" ; + int initial ; + std::cin >> initial ; + std::vector decoded ; + decoded.push_back( initial ) ; + //to find x in a xor x = b you can do a xor b since xor is its own + //inverse function! + for ( auto it = encoded.begin( ) ; it != encoded.end( ) ; ++it ) { + int last = decoded[decoded.size( ) - 1] ; + decoded.push_back( last ^ *it ) ; + } + std::cout << "( " ; + for ( int i : decoded ) { + std::cout << i << ' ' ; + } + std::cout << ")\n" ; + return 0 ; +} + -- cgit