diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2023-03-11 23:20:09 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-03-11 23:20:09 +0000 |
| commit | 993b5cfec02444f8ab158507ea64f42bcd3d175b (patch) | |
| tree | 3678bcde9af7c2fb9a2fcb9f8cd5deefd17f59c2 /challenge-201/paulo-custodio/cpp/ch-2.cpp | |
| parent | 51465a4186e14e5f9ae26e7f5b20dbb9f5ea8aae (diff) | |
| parent | 0ad64a5e5a740655432a36b2af0c35760289cf89 (diff) | |
| download | perlweeklychallenge-club-993b5cfec02444f8ab158507ea64f42bcd3d175b.tar.gz perlweeklychallenge-club-993b5cfec02444f8ab158507ea64f42bcd3d175b.tar.bz2 perlweeklychallenge-club-993b5cfec02444f8ab158507ea64f42bcd3d175b.zip | |
Merge pull request #7709 from pauloscustodio/master
Add Perl, C, C++ and Forth solutions
Diffstat (limited to 'challenge-201/paulo-custodio/cpp/ch-2.cpp')
| -rw-r--r-- | challenge-201/paulo-custodio/cpp/ch-2.cpp | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/challenge-201/paulo-custodio/cpp/ch-2.cpp b/challenge-201/paulo-custodio/cpp/ch-2.cpp new file mode 100644 index 0000000000..9eb7e7a8e7 --- /dev/null +++ b/challenge-201/paulo-custodio/cpp/ch-2.cpp @@ -0,0 +1,52 @@ +/* +Challenge 201 + +Task 2: Penny Piles +Submitted by: Robbie Hatley + +You are given an integer, $n > 0. + +Write a script to determine the number of ways of putting $n pennies in a row +of piles of ascending heights from left to right. +Example + +Input: $n = 5 +Output: 7 + +Since $n=5, there are 7 ways of stacking 5 pennies in ascending piles: + + 1 1 1 1 1 + 1 1 1 2 + 1 2 2 + 1 1 3 + 2 3 + 1 4 + 5 +*/ + +#include <iostream> + +int make_piles1(int prev, int n) { + int count = 0; + if (n == 0) + count++; + else if (n > 0) { + int max = (prev < 0) ? n : prev; + for (int i = 1; i <= max; i++) + count += make_piles1(i, n-i); + } + return count; +} + +int make_piles(int n) { + return make_piles1(-1, n); +} + +int main(int argc, char* argv[]) { + if (argc != 2) { + std::cerr << "usage: ch-2 N" << std::endl; + exit(EXIT_FAILURE); + } + + std::cout << make_piles(atoi(argv[1])) << std::endl; +} |
