Find how many ways to add Please use the c thank you so muc
Find how many ways to add. Please use the c++ , thank you so much!
Ask the user to enter a number and then display how many possible ways one can add up positive numbers to reach this entered value. 3 = 3 3 = 2 + 1 3 = 1 + 1 + 1 Ways to sum to 4: 4 = 4 4 = 3+1 4 = 2 + 2 4 = 2 + 1 + 1 4=1 + 1 + 1 + 1 Example 1 (user input is underlined): Ways to sum to what? 4. 5 Example 2 (user input is underlined): Ways to sum to what? 5. 7 Example 3 (user input is underlined): Ways to sum to what? 10. 42Solution
#include <iostream>
#include <cmath>
using namespace std;
int count( int S[], int m, int n )
{
// If n is 0 then there is 1 solution (do not include any coin)
if (n == 0)
return 1;
// If n is less than 0 then no solution exists
if (n < 0)
return 0;
// If there are no coins and n is greater than 0, then no solution exist
if (m <=0 && n >= 1)
return 0;
// count is sum of solutions (i) including S[m-1] (ii) excluding S[m-1]
return count( S, m - 1, n ) + count( S, m, n-S[m-1] );
}
int main()
{
int number, result;
//cout << \"Enter number : \";
//cin >> number;
number =10;
int n[ number];
// all numbers from 1 to number can be used
for ( int i = 0; i < number; i++ ) {
n[ i ] = i +1;
}
result = count(n,number, number);
cout << result;
return 0;
}
