Combinations without repetition

Apr 13, 2011 at 1:57pm
Hi to all, i am new in this forum.. and i need your help. My problem is that i am making the code for all combinations without repetition but i can't solve it.
My code is about:
- you must put the number n
- put the peaces' number m
- put the peaces in array
- finding all combinations of peace + peace = n not only peace + peace i mean
for all pairs' sums = n.For example: pair of 2 peaces,pair of 3,etc.. but
without repetition of peaces.Only peace1+peace2+peace3 <-one combination,
another peace4+peace5,etc...
-printing the combinations

Thanks.

My code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int temp,i,j,n,m,a[10000],b[10000],c[10000];

    printf("Put the number: ");
    scanf("%d",&n);
    temp=n;
    printf("\nHow much peaces?: ");
    scanf("%d",&m);
    printf("\n");
    for(i=1;i<=m;i++)
    {
        printf("Put the value of peace[No.%d]: ",i);
        scanf("%d",&a[i]);
    }

    for(i=1;i<=m;i++)
    {
        for(j=n;j>=1;j--)
        {
           if(a[i]<=j)
           {
               if(b[j]<a[i]+b[j-a[i]])
               {
                   b[j]=a[i]+b[j-a[i]];
                   c[j]=i;
               }
           }
        }
    }

  i=b[n];
  while(n>0)
  {

        if(temp==n)
        {
                printf("\n*** The best combination is: ");
                printf("(");
            while(i>0)
            {
                 printf("%d,",a[c[i]]);
                 i-=a[c[i]];
            }
        printf(") -> %d less than %d  ***\n\nAnother combinations are:\n",temp-b[n],b[n]);
        }
        else
        {       
                printf("(");
                while(i>0)
                {    
                    printf("%d,",a[c[i]]);
                    i-=a[c[i]];
                    
              }
                
                 printf(") -> %d less than %d\n",temp-b[n],b[n]);

        }
  n--;
  i=b[n];
  }

    system("pause");
    return 0;
}


Apr 13, 2011 at 2:07pm
You may want to look into 'permutations'
Apr 13, 2011 at 2:36pm
my code like this outputs:
i put
n=50
m=6
peace[1]=22,peace[2]=28,peace[3]=16,peace[4]=32,peace[5]=44,peace[6]=5

the output is:
** the best combination is: (22,28,) -> 0 less than 50 **
another combinations are:
(28,16,5,) -> 1 less than 50
(32,16,) -> 2 less than 50
(28,16,) -> 6 less than 50
.
.
so i want when first combination is displayed,then the numbers in that combination to not be used in other combinations.. Can anyone help me where to change and what.. or give my some code or something like that. Thanks
Apr 13, 2011 at 4:06pm
Hey people.. noone know how to solve this problem ?!?!?! :'(
Apr 13, 2011 at 5:08pm
Hey people.. noone know how to solve this problem ?!?!?! :'(


nope =]
Apr 13, 2011 at 8:41pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55

#include <iostream>
#include <vector>

using namespace std;

int main(int argc, char *argv[]) {
	if( argc <= 1 ) {
		cerr << "No arguments.\n";
		return 1;
	}
	
	const int N = argc - 1;
	double *numbers = new double[N];
	for( int i = 1; i < argc; i++ )
		numbers[i - 1] = atof( argv[i] );
	//numbers now has all our numbers
	//N = length of numbers array
	
	vector<vector<double>> combinations;
	for( int len = 1; len <= N; len++ ) {
		//create index array, i
		int *i = new int[len];
		for( int _ = 0; _ < len; _++ )
			i[_] = len - 1 - _;
		
		while( i[0] < N ) {
			vector<double> comb;
			for( int _ = 0; _ < len; _++ )
				comb.push_back( numbers[i[_]] );
			combinations.push_back(comb);
			//iterate i
			for( int _ = 0; _ < len; _++ ) {
				i[_]++;
				if( i[_] < N )
					break;
				if( _ < len - 1 )
					i[_] = i[_ + 1] + 2;
			}
		}
		delete i;
	}
	
	//print combinations
	for( int a = 0; a < combinations.size(); a++ ) {
		cout << "{ ";
		for( int b = 0; b < combinations[a].size(); b++ )
			cout << combinations[a][b] << ' ';
		cout << "}\n";
	}
	
	delete numbers;
	return 0;
}


If you understand it, you can use it :)
i.e Please ask questions!
Apr 13, 2011 at 10:43pm
@Mathhead200 Thank you very much.
I don't understand the code very well because i don't know the 'vector' library,so please tell me where can i put my number =N, how many peaces i have, and the value of each of that peaces?

THANKS
Apr 13, 2011 at 11:11pm
@ OP: Try this http://www.cplusplus.com/reference/algorithm/unique_copy/
The STL has a whole bunch of useful stuff for common tasks like this, a lot of schools just don't teach them for some reason until later.
Apr 14, 2011 at 1:14am
Well, think of vector<vector<double>> as double[][], except vectors are variable length. If your number are { 1, 2, 3 }, after the program runs, the vector will contain
{
  { 1 }
  { 2 }
  { 3 }
  { 1 2 }
  { 1 3 }
  { 2 3 }
  { 1 2 3 }
}


For more information on vectors, check this site. They are (in a way) just another type of array.
Topic archived. No new replies allowed.