aboutsummaryrefslogtreecommitdiff
path: root/challenge-075
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2020-08-28 15:51:39 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2020-08-28 15:51:39 +0100
commit8d4abcb691fa0008a123f302f03f684f8ffd2ed2 (patch)
tree91e3ab91f1fe961240b7f9ab6cabb2136874673a /challenge-075
parent4e01dc493388e093a1b638b3001eb17fa86a16e0 (diff)
downloadperlweeklychallenge-club-8d4abcb691fa0008a123f302f03f684f8ffd2ed2.tar.gz
perlweeklychallenge-club-8d4abcb691fa0008a123f302f03f684f8ffd2ed2.tar.bz2
perlweeklychallenge-club-8d4abcb691fa0008a123f302f03f684f8ffd2ed2.zip
- Added C++ solution by Ulrich Rieke.
Diffstat (limited to 'challenge-075')
-rw-r--r--challenge-075/ulrich-rieke/cpp/ch-2.cpp80
1 files changed, 80 insertions, 0 deletions
diff --git a/challenge-075/ulrich-rieke/cpp/ch-2.cpp b/challenge-075/ulrich-rieke/cpp/ch-2.cpp
new file mode 100644
index 0000000000..e91014c69a
--- /dev/null
+++ b/challenge-075/ulrich-rieke/cpp/ch-2.cpp
@@ -0,0 +1,80 @@
+#include <vector>
+#include <iostream>
+#include <algorithm>
+
+std::vector<std::vector<int>> createGroups( const std::vector<int> &
+ numbers , int groupwidth ) {
+ std::vector<std::vector<int>> groups ;
+ int len = numbers.size( ) ;
+ std::vector<int> group ;
+ for ( int start = 0 ; start < len - groupwidth + 1 ; start++ ) {
+ int current = start ;
+ while ( current < (start + groupwidth) ) {
+ group.push_back( *(numbers.begin( ) + current) ) ;
+ current++ ;
+ }
+ groups.push_back( group ) ;
+ group.clear( ) ;
+ }
+ return groups ;
+}
+
+void printArray( const std::vector<int> & array ) {
+ int maximum = *std::max_element( array.begin( ) , array.end( ) ) ;
+ int len = array.size( ) ;
+ int current = maximum ;
+ while ( current > 0 ) {
+ std::cout << current ;
+ for ( int i = 0 ; i < len ; i++ ) {
+ if ( array[ i ] >= current )
+ std::cout << " #" ;
+ else
+ std::cout << " " ;
+ }
+ std::cout << '\n' ;
+ current-- ;
+ }
+ for ( int i = 0 ; i < len + 1 ; i++ ) {
+ std::cout << "- " ;
+ }
+ std::cout << '\n' ;
+ std::cout << " " ;
+ for ( int num : array )
+ std::cout << num << ' ' ;
+ std::cout << '\n' ;
+}
+
+std::vector<int> enterArray( ) {
+ std::cout << "Enter numbers ( 0 to end ) !\n" ;
+ std::vector<int> array ;
+ int num ;
+ std::cin >> num ;
+ while ( num != 0 ) {
+ array.push_back( num ) ;
+ std::cin >> num ;
+ }
+ return array ;
+}
+
+int main( ) {
+ std::vector<int> array = enterArray( ) ;
+ std::vector<std::vector<int>> allNeighbouringGroups ;
+ int len = array.size( ) ;
+ for ( int w = 2 ; w < len ; w++ ) {
+ std::vector<std::vector<int>> groups( createGroups( array, w ) ) ;
+ for ( const auto & myGroup : groups ) {
+ allNeighbouringGroups.push_back( myGroup ) ;
+ }
+ }
+ std::vector<int> areas ;
+ for ( const auto & myGroup : allNeighbouringGroups ) {
+ areas.push_back( *std::min_element( myGroup.begin( ) , myGroup.end( ))
+ * myGroup.size( ) ) ;
+ }
+ int totalMax = std::max( *std::max_element( areas.begin( ) , areas.end( ) ),
+ *std::min_element( array.begin( ) , array.end( ) ) * len ) ;
+ std::cout << "maximum rectangle area is " << totalMax << " !\n" ;
+ std::cout << std::endl ;
+ printArray( array ) ;
+ return 0 ;
+}