首页 > 代码库 > 递归的再度思索-多个数组路劲遍历

递归的再度思索-多个数组路劲遍历

类型:

 

1.       SHA-AA/BA/KX/WC-NYC/TYO/OSA-BA/NX/CX-NYC

2.       SHA -NYC/TYO/OSA -NYC

/之间区分的长度不一

想要的结果SHA-AA-NYC-BA-NYC

SHA-AA-NYC-NX-NYC

SHA-AA-NYC-CX-NYC

SHA-AA-NYC-CX-NYC

SHA-AA-TYO-BA-NYC

SHA-AA- TYO-NX-NYC

SHA-AA- TYO-CX-NYC

SHA-AA- TYO-CX-NYC

第一条有排列组合36种



思路


private void button1_Click(object sender, EventArgs e)
        {
            string txtOrg = textBox1.Text.Trim();

            string[] t1 = txtOrg.Split('-');
            //if ((t1.Length & 1) != 1)
            //{
            //    MessageBox.Show("录入数据有问题");
            //    return;
            //}

            StringBuilder sb = new StringBuilder("");
            GetStringRecursive(sb, "", t1.ToList());

            textBox2.Text = "广告位招租,欲去除请付我5元钱\r\n" + sb.ToString();

        }


        private string GetStringRecursive(StringBuilder preBf, string pre, List<string> listStr)
        {
            //List<string> listRet = new List<string>();
            string retStr = "";
            if(listStr.Count == 0)
                return pre + "\r\n";
            else{
                List<string> listTmp = listStr[0].Split('/').ToList();
                List<string> listTmp2 = listStr.FindAll(x => x != listStr[0]);
                bool isOver = listTmp2.Count == 0;

                for (int i = 0; i <= listTmp.Count - 1; i++)
                {
                    string strTmp = listTmp[i].Trim();
                    //string[] = strTmp[i]
                    retStr = pre + strTmp;
                    //listStr.Remove()
                    if (isOver)
                    {
                        preBf.Append(retStr + "\r\n");
                    }
                    else
                    {
                        GetStringRecursive(preBf, retStr + '-', listTmp2);
                    }
                }
            }

            return retStr;
        }