Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to start for solve this exercise in c

Tags:

c

set

Hello I resort to your help with the intention of indicating me, what are the steps that I must follow to solve this exercise make it clear that I am not asking for the solution to the problem

Given a set {1...N} We can divide it into two subsets that their sum give the same, for example for N = 3:

{1,2} = {3}

Another example with N = 7:

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

Given an N, calculate how many ways we can make the subsets for this property is fulfilled, for N = 3 we have seen that there is a possibility; for N = 7 we have 4 possibilities. Make a recursive algorithm that solves for any 0 < N < 39.

Example input:
7
The function must be given:
3
Example input 2:
3
Example output 2
1

any aid would be welcome

edit

#include<stdio.h>

int count( int S[], int m, int n )
{
     if (n == 0)
       return 1;


    if (n < 0)
        return 0;
    if (m <=0 && n >= 1)
        return 0;
    return count( S, m - 1, n ) + count( S, m, n-S[m-1] );
}
int main()
{
    int arr[] = {1,2};
    int m = sizeof(arr)/sizeof(arr[0]);
    printf("%d ", count(arr, m, 3));

    return 0;
}

in this case two gives me instead of one it's wrong

like image 239
cheroky Avatar asked Dec 07 '25 07:12

cheroky


1 Answers

You could use the fact that the sum of the values is n*(n+1)/2. Your task is to find all the sets with n*(n+1)/4. to do this you can use the change-making algorithm (it is recursive as your require). Your coins are the integers for 1 to N and the money you want to distribute is n*(n+1)/4.

Source : http://en.wikipedia.org/wiki/Change-making_problem

like image 137
DARK_DUCK Avatar answered Dec 08 '25 21:12

DARK_DUCK