diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2021-04-13 19:47:34 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2021-04-13 19:47:34 +0100 |
| commit | 94ae236a562790b8a38b17614f97e2eded3bcba4 (patch) | |
| tree | 1ca1e5d1f336734b3511122a88c337ae0d398ad8 /challenge-108/ulrich-rieke/cpp/ch-2.cpp | |
| parent | 4f5a6326cd97c1aa247d1c974e9730696922bfc0 (diff) | |
| download | perlweeklychallenge-club-94ae236a562790b8a38b17614f97e2eded3bcba4.tar.gz perlweeklychallenge-club-94ae236a562790b8a38b17614f97e2eded3bcba4.tar.bz2 perlweeklychallenge-club-94ae236a562790b8a38b17614f97e2eded3bcba4.zip | |
- Added solutions by Ulrich Rieke.
Diffstat (limited to 'challenge-108/ulrich-rieke/cpp/ch-2.cpp')
| -rw-r--r-- | challenge-108/ulrich-rieke/cpp/ch-2.cpp | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/challenge-108/ulrich-rieke/cpp/ch-2.cpp b/challenge-108/ulrich-rieke/cpp/ch-2.cpp new file mode 100644 index 0000000000..5d39d4793d --- /dev/null +++ b/challenge-108/ulrich-rieke/cpp/ch-2.cpp @@ -0,0 +1,26 @@ +#include <vector> +#include <iostream> + +int main( ) { + std::vector<int> bellnumbers { 1 } ; + std::vector<int> currentRow ; + std::vector<int> previousRow { 1 } ; + while ( bellnumbers.size( ) < 10 ) { + currentRow.push_back( previousRow.back( )) ; + int currentIndex = 0 ; + while ( currentRow.size( ) < ( previousRow.size( ) + 1 ) ) { + currentIndex++ ; + currentRow.push_back( currentRow[ currentIndex - 1 ] + + previousRow[ currentIndex - 1 ] ) ; + } + bellnumbers.push_back( *(currentRow.begin( ) ) ) ; + previousRow = currentRow ; + currentRow.clear( ) ; + } + std::cout << "These are the first 10 Bell numbers:\n" ; + for ( int i = 1 ; i < 10 ; i++ ) { + std::cout << " " << i << " : " << bellnumbers[ i - 1 ] << '\n' ; + } + std::cout << 10 << " : " << bellnumbers[9] << '\n' ; + return 0 ; +} |
