排列保存到磁盘

时间:2012-08-24 03:58:22

标签: c# permutation combinations

我有一个程序需要进行许多排列并保存它们 P型有6个字符,N型有5个字符,C有23个可能的选项 组合必须是

形式
A=PNPPNNPPNPPNNP
B=PNPNPNPNPNPNPN

then save these to ouput file:
A0CCCCA0CCCCA0CCCCA0
...
AnCCCCA0CCCCA0CCCCA0
...
AnCCCCAnCCCCA0CCCCA0
...
AnCCCCAnCCCCBnCCCCAn
...
AnCCCCAnCCCCBnCCCCBn
....
BnCCCCBnCCCCBnCCCCBn

我写了一个程序,它创建了PNPPNNPPNPPNNP的所有可能组合,并将它们存储在一个数组中,但是我的记忆在它完成之前就已经填满了。

    int current=0;
    int p1=0;
    string temp="";
    while(p1<6)
    {
        string temp1=polar[p1];

        //NPPNNPPNPPNNP
        int n1=0;
        while(n1<5)
        {
            string temp2=nonpolar[n1];
            //PPNNPPNPPNNP
            int p2=0;
            while(p2<6)
            {
                string temp3=polar[p2];
                //PNNPPNPPNNP
                int p3=0;
                while(p3<6)
                {
                    string temp4=polar[p3];
                    //NNPPNPPNNP
                    int n2=0;
                    while(n2<5)
                    {
                        string temp5=nonpolar[n2];
                        //NPPNPPNNP
                        int n3=0;
                        while(n3<5)
                        {
                            string temp6=nonpolar[n3];
                            //PPNPPNNP
                            int p4=0;
                            while(p4<6)
                            {
                                string temp7=polar[p4];
                                //PNPPNNP
                                int p5=0;
                                while(p5<6)
                                {
                                    string temp8=polar[p5];
                                    //NPPNNP
                                    int n4=0;
                                    while(n4<5)
                                    {
                                        string temp9=nonpolar[n4];
                                        //PPNNP
                                        int p6=0;
                                        while(p6<6)
                                        {
                                            string temp10=polar[p6];
                                            //PNNP
                                            int p7=0;
                                            while(p7<6)
                                            {
                                                string temp11=polar[p7];
                                                //NNP
                                                int n5=0;
                                                while(n5<5)
                                                {
                                                    string temp12=nonpolar[n5];
                                                    //NP
                                                    int n6=0;
                                                    while(n6<5)
                                                    {
                                                        string temp13=nonpolar[n6];
                                                        //P
                                                        int p8=0;
                                                        while(p8<6)
                                                        {
                                                            string temp14=polar[p8];

                                                            a[current]=temp1+temp2+temp3+temp4+temp5+temp6+temp7+temp8+temp9+temp10+temp11+temp12+temp13+temp14;
                                                            current=current+1;
                                                            p8=p8+1;
                                                        }
                                                        n6=n6+1;
                                                    }
                                                }
                                                n5=n5+1;
                                            }
                                            p6=p6+1;
                                        }
                                     n4=n4+1;   
                                    }
                                    p5=p5+1;
                                }
                                p4=p4+1;
                            }
                            n3=n3+1;
                        }
                        n2=n2+1;
                    }
                    p3=p3+1;
                }
                p2=p2+1;
            }
            n1=n1+1;
        }
        p1=p1+1;
    }

谢谢!

示例的进一步代码

    string[] polar = new string[10];
    polar[0]="H";
    polar[1]="Q";
    polar[2]="N";
    polar[3]="K";
    polar[4]="D";
    polar[5]="E";
    string[] nonpolar = new string[10];
    nonpolar[0]="F";
    nonpolar[1]="L";
    nonpolar[2]="I";
    nonpolar[3]="M";
    nonpolar[4]="V";
    string[] all = new string[100];
    all[0]="A";
    all[1]="B";
    all[2]="C";
    all[3]="D";
    all[4]="E";
    all[5]="F";
    all[6]="G";
    all[7]="H";
    all[8]="I";
    all[9]="J";
    all[10]="K";
    all[11]="L";
    all[12]="M";
    all[13]="N";
    all[14]="O";
    all[15]="P";
    all[16]="Q";
    all[17]="R";
    all[18]="S";
    all[19]="T";
    all[20]="U";
    all[21]="V";
    all[22]="W";
    all[23]="Y";

我当前的代码生成HFHHFFHHFHHFFH作为第一个正确的alpha实例,但我需要让它生成

A0CCCCA0CCCCA0CCCCA0 HFHHFFHHFHHFFHAAAAHFHHFFHHFHHFFHAAAAHFHHFFHHFHHFFHAAAAHFHHFFHHFHHFFH

作为第一个输出后跟 A0CCCCA0CCCCA0CCCCA1 HFHHFFHHFHHFFHAAAAHFHHFFHHFHHFFHAAAAHFHHFFHHFHHFFHAAAAHFHHFFHHFHHFFQ

所有可能的排列,包括Cs随着投入混合物的所有测试版而变化

1 个答案:

答案 0 :(得分:0)

似乎你需要具有特定排序的排列......一般来说,你的代码应该更像这样:

var polar=new string[]{"A", "B"};
var nonPolar=new string[]{"a", "b"};
var all = new string[]{"a", "b"};
var query=from pos1 in polar 
from pos2 in nonPolar 
from pos3 in all
select pos1 + pos2 + pos3;
  foreach (var element in query)
                Console.WriteLine(element);
相关问题