combinations programm

Discussion in 'C' started by vagelis, Dec 12, 2010.

  1. vagelis

    vagelis New Member

    Joined:
    Dec 12, 2010
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    hello again..

    i want to make a programm to find all the combinations of n numbers choosing the k of them

    for example

    [1 2 3 4 5]

    the combinations of two of them are

    1 2
    1 3
    1 4
    1 5
    2 3
    2 4
    2 5
    3 4
    3 5
    4 5

    the combinations of 3 numbers of them are

    1 2 3
    1 2 4
    1 2 5
    1 3 4
    1 3 5
    1 4 5
    2 3 4
    2 3 5
    2 4 5
    3 4 5



    i have made this programm that it is close to find the write solution but i cannot go farther

    please i want help..
    if you can improve my programm or suggest me something already done..

    Code:
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]
    #include[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"stdafx.h"
    [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]#include[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"iostream"
    [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]#include[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]<vector>
    [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]using[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]namespace[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] std;
    vector<vector<[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]>> v;
    [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] arithmos_comb_sinolikos;
    [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]void[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] comb(vector<[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]> A,[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] k,vector<vector<[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]>> &v,[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] column_v,[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] row);
    [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] arithmos_comb_epomeno([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] n,[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] k);
    [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] factorial([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] number) ;
    ostream & print([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]const[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] vector<[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]> & v);
    ostream & print([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]const[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] vector<vector<[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]> > & v);
    [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] _tmain([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] argc, _TCHAR* argv[])
    {
    [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] arr[]={1,4,6,7,9};
    [/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]//cout<<arr[0];
    [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]vector<[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]> A(arr,arr+5);
    v.push_back(vector<[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]>());
    [/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]//cout<<v.size();
    [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]//cout<<A[3];
    [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]arithmos_comb_sinolikos=arithmos_comb_epomeno(A.size(),2);
    [/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]//cout<<arithmos_comb_epomeno(4,2);
    [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]//cout<<arithmos_comb_sinolikos;
    [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]comb(A,2,v,0,0);
    [/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]//print(v);
    [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]cin>>arr[0];
    }
    [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]void[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] comb(vector<[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]> A,[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] k,vector<vector<[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]>> &v,[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] column_v,[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] row)
    {
    [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2](row>=arithmos_comb_sinolikos)
    {
    [/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]//cout<<"epistrefei_giati_row>=arithmos_comb"<<endl;
    [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]//return;
    [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]}
    [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]else
    [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]{
    }
    cout<<endl<<endl<<[/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"mpike_sto_comb"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];
    print(v);
    [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]for[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] i=column_v;i<=A.size()-k+column_v;i++)
    {
    cout<<[/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"mpike_sto_proto_for i="[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]<<i<<endl;
    [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]for[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] y=0;y<arithmos_comb_epomeno(A.size()-column_v-i-1,k-column_v-1);y++)
    {
    cout<<[/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"mpike_sto_deytero_for y="[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]<<y<<endl;
    v[v.size()-1].push_back(A[i]);
    print(v);
    cout<<[/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"edaxei"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]<<endl;
    cout<<[/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"column_v="[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]<<column_v<<endl;
    
    [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2](column_v+1<k)
    {
    cout<<[/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"mpike_sto_mikrotero_k"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]<<endl;
    comb(A,k,v,column_v+1,row);
    [/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]//return;
    [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]
    
    }
    [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]else
    [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]{
    cout<<[/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"mpike_sto_megalitero_k"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]<<endl;
    [/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]//v[v.size()-1].push_back(A[i+column_v]);
    [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]v.push_back(vector<[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]> ());
    column_v=0;
    [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]return[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];
    
    [/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]//comb(A,k,v,0,stili_A,row+1);
    [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]//return;
    [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]
    }
    
    
    
    }
    
    }
    }
    [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] arithmos_comb_epomeno([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] n,[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] k)
    { 
    
    [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]return[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] (factorial(n)/(factorial(k)*factorial(n-k)));
    }
    [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] factorial([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] number) {
    [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] temp;
    [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2](number <= 1) [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]return[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] 1;
    temp = number * factorial(number - 1);
    [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]return[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] temp;
    }
    ostream & print([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]const[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] vector<[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]> & v)
    {
    [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]for[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] ([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] i=0; i<v.size(); i++)
    cout << v[i] << [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]' '[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];
    [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]return[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] cout;
    }
    ostream & print([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]const[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] vector<vector<[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]> > & v)
    {
    [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]for[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] ([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] i=0; i<v.size(); i++)
    {
    [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]for[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] ([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] j=0; j<v[i].size(); j++)
    cout << v[i][j] << [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]' '[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];
    cout << endl;
    }
    [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]return[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] cout;
    }
    [/SIZE]
     
  2. jimblumberg

    jimblumberg New Member

    Joined:
    May 30, 2010
    Messages:
    120
    Likes Received:
    29
    Trophy Points:
    0
    Does your program compile without errors/warnings?

    If not please post the complete error/warning messages.

    If it does compile, what is your program doing wrong?

    What were your inputs? What did the program output?

    What did you expect?

    Jim
     
  3. vagelis

    vagelis New Member

    Joined:
    Dec 12, 2010
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    it compiles but here is the difference of what i want and what i get

    must be ------and i find
    1 4--------------1 4
    1 6--------------1 4
    1 7--------------1 4
    1 9--------------1 4
    4 6--------------4 4
    4 7--------------4 4
    4 9--------------4 4
    6 7--------------6 4
    6 9--------------6 4
    7 9--------------7 4

    it is difficult to do this
    it is difficult the logic..i think only a very good programmer could do that..
    and i am not
     
  4. vagelis

    vagelis New Member

    Joined:
    Dec 12, 2010
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    the numbers are 1,4,6,7,9 and i want the combinations with k=2
     
  5. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    If you want a fixed depth then do it with nested for loops which is by far the simplest solution, e.g.
    Code:
    for (int i=0; i<5; i++)
    {
      for (int j=0; j<5; j++)
      {
        if (i!=j)
        {
          printf("%d %d\n",arr[i], arr[j]);
        }
      }
    }
    
    I'll leave it to you to work out how to eliminate duplicates from this; it's not difficult.

    Variable depth is more tricky but can be solved with recursion and a second array to mark whether or not a number has been "seen":

    Code:
    int arr[]={1,4,6,7,9};
    int seen[5];
    int maxdepth=3;
    
    void recurse(int depth)
    {
    	if (depth>=maxdepth)
    	{
    		for (int i=0; i<5; i++)
    		{
    			if (seen[i])
    				printf("%d ",arr[i]);
    		}
    		printf("\n");
    	}
    	else
    	{
    		for (int i=0; i<5; i++)
    		{
    			if (!seen[i])
    			{
    				seen[i]=1;
    				recurse(depth+1);
    				seen[i]=0;
    			}
    		}
    	}
    }
    
    void test38()
    {
    	for (int i=0; i<5; i++)
    	{
    		seen[i]=0;
    	}
    
    	for (int i=0; i<5; i++)
    	{
    		seen[i]=1;
    		recurse(1);
    		seen[i]=0;
    	}
    }
    
    Again this includes duplicates, and similarly I'll leave it as an exercise for you to determine how best to eliminate them. Another solution is to use an array of counters, which is essentially the same as this code where the "array" is effectively stored on the stack.
     
  6. vagelis

    vagelis New Member

    Joined:
    Dec 12, 2010
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    i found this that works..

    Code:
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]
    template[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] <[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]class[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] RanIt, [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]class[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Func>
    [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]void[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] recursive_combination(RanIt nbegin, RanIt nend, [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] n_column,
    RanIt rbegin, RanIt rend, [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] r_column,[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] loop, Func func,vector<vector<[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]>> &comb)
    {
    [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] r_size=rend-rbegin;
    [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] localloop=loop;
    [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] local_n_column=n_column;
    [/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]//A different combination is out
    [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2](r_column>(r_size-1))
    {
    comb.push_back(vector<[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]>());
    func(rbegin,rend,comb);
    [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]return[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];
    }
    [/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]//===========================
    [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]for[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] i=0;i<=loop;++i)
    {
    RanIt it1=rbegin;
    [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]for[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] cnt=0;cnt<r_column;++cnt)
    {
    ++it1;
    } 
    RanIt it2=nbegin;
    [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]for[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] cnt2=0;cnt2<n_column+i;++cnt2)
    {
    ++it2;
    } 
    *it1=*it2;
    ++local_n_column;
    recursive_combination(nbegin,nend,local_n_column,
    rbegin,rend,r_column+1,localloop,func,comb);
    
    --localloop;
    }
    }
    [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]typedef[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] vector<[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]>::iterator vii;
    [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]void[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] display(vii begin,vii end,vector<vector<[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]>> &comb)
    {
    [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]for[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] (vii it=begin;it!=end;++it)
    {
    [/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]//cout<<*it;
    [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]comb[comb.size()-1].push_back(*it);
    }
    [/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]//cout<<endl;
    [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]}
     
    int main()
    {
               [SIZE=2]
    vector<[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]> linspace;
    [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]for[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] i=0;i<Mem_in.size();i++) linspace.push_back(i+1); 
    
    vector<[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]> cb;
    [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]for[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] i=0;i<k;i++) cb.push_back (i+1);
    
    vector<vector<[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]>> v;
    recursive_combination(linspace.begin (),linspace.end(),0,cb.begin(),cb.end(),0,linspace.size()-k,display,v);
    //and we find v that has the combinations..
    [/SIZE] 
    }
    [/SIZE]
     
  7. jimblumberg

    jimblumberg New Member

    Joined:
    May 30, 2010
    Messages:
    120
    Likes Received:
    29
    Trophy Points:
    0
    So you:
    Do you know why it works?

    Since the program you "found" is using advanced language features, don't you think your instructor will recognize that this is not your work?

    Jim
     
  8. vagelis

    vagelis New Member

    Joined:
    Dec 12, 2010
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    my friend my work is not to do this but i must use this to do my work..
    thank you for your time and for your help!!
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice