hektor wrote:
> my datas are
>
> A  X 3 ( there are three "A" )
> B X 5
> C X 2
>
> i want to see all possible addition like
>
> A
> A + A
> A + A + A
> A + B
> A + A + B
> A + A + A + B
> A + B + C
> ...
> ...
> ...
>
> how can i do that ?
>
> thanks

/*
Iterative soln
This forms the basis for recursive soln

#include<stdio.h>
int main()
{
        int i,j,k;

        for(i=0;i<4;i++)
        for(j=0;j<6;j++)
        for(k=0;k<3;k++)
        {
                printf("%d %d %d\n",i,j,k);
        }

        return 0;
}

*/



#include<stdio.h>

int arr[100];
int limits[3]={3,5,2};

int count=0;

void recur(int level)
{
        int i;

        if(level==3){

        for(i=0;i<3;i++)
                printf("%d ",arr[i]);
        printf("\n");
                return;
        }

        for(i=0;i<=limits[level];i++)
        {
                arr[level]=i;
                recur(level+1);
        }
}

int main()
{
        recur(0);
        return 0;
}

you get an array like 3 5 2 , or 1 1 1
the first column is number of A's,2nd column number of B's and 3rd
column number of C's.
So i think you can write a mapping easily and change the change 3 to n
and arr accordingly.

Reply via email to