From b2ad01584238b6bf7603faeb2748d93d431345a6 Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Tue, 5 Apr 2022 21:38:59 +0100 Subject: - Added solutions by Ulrich Rieke. --- challenge-159/ulrich-rieke/cpp/ch-1.cpp | 35 +++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 challenge-159/ulrich-rieke/cpp/ch-1.cpp (limited to 'challenge-159/ulrich-rieke/cpp/ch-1.cpp') diff --git a/challenge-159/ulrich-rieke/cpp/ch-1.cpp b/challenge-159/ulrich-rieke/cpp/ch-1.cpp new file mode 100644 index 0000000000..fd619c7af4 --- /dev/null +++ b/challenge-159/ulrich-rieke/cpp/ch-1.cpp @@ -0,0 +1,35 @@ +#include +#include +#include +#include +#include +#include + +int main( int argc, char * argv[] ) { + int n = std::atoi( argv[1] ) ; + while ( n <= 0 ) { + std::cout << "Please enter an integer greater than 0!\n" ; + std::cin >> n ; + } + std::vector numerators( n ) ; + std::vector denominators( n + 1 ) ; + std::iota( numerators.begin( ) , numerators.end( ) , 1) ; + std::iota( denominators.begin( ) , denominators.end( ) , 0 ) ; + std::vector> fractions ; + fractions.push_back( std::make_pair( 0 , 1 ) ) ; + for ( int i : numerators ) { + for ( int j : denominators ) { + if ( i < j && (std::lcm( i , j ) == i * j)) { + fractions.push_back( std::make_pair( i , j )) ; + } + } + } + std::sort( fractions.begin( ) , fractions.end( ) , + []( const auto & a, const auto & b ) { return + (static_cast(a.first) / static_cast(a.second)) < + (static_cast(b.first) / static_cast(b.second));} ); + for ( auto & p : fractions ) + std::cout << p.first << '/' << p.second << ", " ; + std::cout << "1/1" << std::endl ; + return 0 ; +} -- cgit