Trying to write a recursive function that counts the number of sequences summing up to this C ++ number

Ok, that’s what I'm trying to do. The user enters a number. I am trying to write a recursive function that counts the number of sequences that add up to this number (user input).

For instance:

Then the number of sequences summing up to 6 is 11 (including 6 itself).

6
5+1
4+1+1
3+1+1+1
2+1+1+1+1
1+1+1+1+1+1
2+2+1+1
3+2+1
4+2
2+2+2
3+3

I also try not to repeat the sequences, for example 2 + 2 + 1 + 1 and 1 + 1 + 2 + 2.

The reason I don't have the code is because I can't figure out a recursive way to make this work, so I'm looking for some recommendations. Thanks in advance!

Addition:

Okay, that’s what my thinking process is. 6 can be divided into ...

6
5+1
4+2
3+3

, 5 + 1 , +1 ; , .

4+1+1
3+2+1

..... , .

, , , . , .

int sum(int number, int min, int counter)
{
    int temp=0, n;
    n=number+temp;
    if (number>=(n/2)& number!=min)
    {
        while (number>=(n/2))
        {
            cout << number << "+"<< temp <<"\n";
            number --;
            temp ++;
            counter ++;
        }
    }
    sum(temp, 1,counter);
    return counter;
}

int main()
{
    int number;

    cout << "Please enter the number: ";
    cin >> number ;
    cout << "\n";

    sum(number, 1, 0);

    return 0;
}

, .

+3
6

: , n k.

EDIT:
, , ... - . , . , .

: , "2 2 1 1 1 1". , 6? , , 1, , 2, 6, . 2 ? ( .) 2, - 4 , 2, , 4 , 2. , , , .

EDIT:
, , :

int partition(int n, int max)
{
  if(n==0)
    return(0);
  int ret = 0;
  if(n<=max)
    ret=1;
  for(...)
  {
    ...
  }
  return(ret);
}

?

+4

,
{1,2,3,4,5,6}, 6

0

, , . : Integer .

0

f (n) - , , n,

f (n) = g (n, n)

g (n, p) = {i\in 1..min(n, p): [i g (n-i, i)]}

0

P (n) n, n , n >= 1.

p (n, k) n , k, k , k >= 1, k <= n.

, P (n) = sum (i: 1..n) p (n, i).

p (n, k), , , : . , 1 k, n , .

, p (n, k) = sum (i: 1..k) p (n - i, i).

p (1, 1) = 1.


, , ( ) - !

// A utility function to represent the result of appending to a vector,
// as a new vector (without affecting the previous one).
template <typename T>
vector<T> operator<<(vector<T> copy, T to_append) {
    // We passed by value to get a copy implicitly.
    copy.push_back(to_append);
    return copy;
}

// A utility function to append one vector to another.
template <typename T>
vector<T>& operator+=(vector<T>& original, const vector<T>& to_append) {
    // 'copy' is in namespace std:: and defined in <algorithm>.
    // 'back_inserter' is in namespace std:: and defined in <iterator>.
    copy(to_append.begin(), to_append.end(), back_inserter(original));
    return original;
}

vector<vector<int> > partitions(int remaining, int limit, vector<int> prefix) {
    // Finds the partitions of some larger number which start with the
    // numbers in 'prefix', such that the rest of the "chunks" sum to
    // 'remaining' and are all no larger than 'limit'.

    // 'max' is in namespace std:: and defined in <algorithm>. We restrict
    // the 'limit' to be no more than 'remaining'.
    limit = max(remaining, limit);

    vector<vector<int> > result;

    // Base case. 
    if (remaining == 1) {
        return result << (prefix << 1); // note the parentheses are required!
    }

    for (int i = limit; i > 0; --i) {
        result += partitions(remaining - i, i, prefix << i);
    }

    return result;
}
0
0

Source: https://habr.com/ru/post/1779109/


All Articles