[try Beta version]
Not logged in

 
How to find possible combination

Jan 2, 2012 at 11:05am
Hi,
I want to find possible combination of "TEST", which is char array of five indexes.There should be no repetition of chars.
For example
TEST should be out one time, but not two time because 0th index and 3th index are same.
My code is showing it two time.
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
#include<iostream>
using namespace std;
int  main()
{
    int i,j,k,l,m;
    char ch[5]="TEST";

    for(i=0;i<4;i++)
    {
        for(j=0;j<4;j++)
        {
            for(k=0;k<4;k++)
            {
                for(l=0;l<4;l++)
                {
                    if(i!=j&&i!=k&&i!=l&&j!=k&&j!=l&&k!=l)
                    {
                        cout<<ch[i]<<ch[j]<<ch[k]<<ch[l]<<endl;
                    }
                 }
            }
        }
    }
return 0;
}

Jan 2, 2012 at 11:34am
What do you think about:
http://en.wikipedia.org/wiki/Permutation ?
I think it can help to define this permutation amount.
Jan 2, 2012 at 11:34am
The problem is recursive. All permutations of TEST are (T + all permutations of EST) + (E + all permutations of TST) + (S + all permutations of TET). The challenge is to find all unique characters in a string. Sorting it would help..
Feb 21, 2012 at 9:57am
here:
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
#include<iostream>
#include <cstdlib>
using namespace std;
int  main()
{
    int i,j,k,l,m;
    char ch[5]="TEST";
    cout<<"TEST"<<endl;
    for(i=0;i<4;i++)
    {
        for(j=0;j<4;j++)
        {
            for(k=0;k<4;k++)
            {
                for(l=0;l<4;l++)
                {
                    if(i!=j&&i!=k&&i!=l&&j!=k&&j!=l&&k!=l)
                    {
                        if (!(ch[i]==ch[l] && ch[j]=='E')) cout<<ch[i]<<ch[j]<<ch[k]<<ch[l]<<endl;
                    }
                 }
            }
        }
    }
system ("pause");
return 0;
}

this should do it. infact, it DOES do it, I tested!
Last edited on Feb 21, 2012 at 9:57am
Topic archived. No new replies allowed.