求 0, 1, 7, 2, 3, 2, 4, 0 这8个数字中 所有6位数的组合 0在第一位也算麻烦高手贴出代码 谢谢啦

解决方案 »

  1.   


    // 因结果集较大,写到了文件中.
    private void button1_Click(object sender, EventArgs e)
    {
        Char[] chr = "01723240".ToCharArray();    File.WriteAllText(@"C:\file0.txt", "", Encoding.Default);
        for (int i = 0; i < chr.Length; i++)   // 处理 i个字符长度的
        {
            StreamReader sr = new StreamReader(@"C:\file" + i + ".txt", Encoding.Default);
            StreamWriter sw = new StreamWriter(@"C:\file" + (i + 1) + ".txt", false, Encoding.Default);
            string str = "";
            while (true)
            {
                str = sr.ReadLine();
                if (str == null && i != 0) { break; }
                if (i == 0) { str = ""; }
                for (int j = 0; j < chr.Length; j++)
                {
                    sw.WriteLine(str + chr[j].ToString());
                }
                if (i == 0) { break; }
            }
            sr.Close();
            sw.Close();
        }
        if (i == 6) break;  //计算到第6位
    }C:\file6.txt  文件中是全排列结果
      

  2.   

    很感谢 lzsh0622 的回答但是出现数字重复 实际情况中 除了0和2出现两次其他数字只出现1次继续在线等 谢谢啦
      

  3.   

    http://topic.csdn.net/u/20090217/21/F41ED9F6-F929-451C-A5C9-80D2E408422A.html
      

  4.   

    试试下面的代码,做过重复控制:public bool getAllString(string inputString, int len)
    {
        if (len > inputString.Length) { return false; }
        try
        {
            Char[] chr = inputString.ToCharArray();        File.WriteAllText(@"C:\file0.txt", "", Encoding.Default);
            for (int i = 0; i < len; i++)                      // 处理 i个字符长度的 
            {
                StreamReader sr = new StreamReader(@"C:\file" + i + ".txt", Encoding.Default);
                StreamWriter sw = new StreamWriter(@"C:\file" + (i + 1) + ".txt", false, Encoding.Default);
                string str = "";
                while (true)
                {
                    str = sr.ReadLine();
                    if (str == null && i != 0) { break; }
                    if (i == 0) { str = ""; }
                    for (int j = 0; j < chr.Length; j++)
                    {
                        // 保证0和2出现两次,其他数字只出现1次                    Regex reg = new Regex(chr[j].ToString(), RegexOptions.Singleline);
                        switch (chr[j])   
                        {
                            case '0':
                            case '2':
                                if (reg.Matches(str).Count <=1)
                                {
                                    sw.WriteLine(str + chr[j].ToString());
                                }
                                break;
                            default:
                                if (reg.Matches(str).Count <=0)
                                {
                                    sw.WriteLine(str + chr[j].ToString());
                                }
                                break;
                        }
                    }
                    if (i == 0) { break; }
                }
                sr.Close();
                sw.Close();
            }
            File.Delete(@"C:\file0.txt");
        }
        catch (Exception ex) { MessageBox.Show(ex.ToString()); return false; }
        return true;
    }// 测试用例 
    private void button1_Click(object sender, EventArgs e)
    {
        getAllString("01723240", 6);
    }
      

  5.   

    给个比较土的代码吧,标准的方法应该是用字典序或临位交换法。
    using System;namespace CsdnTest
    {
        class Program
        {
            static char[] Items;
            static int TLength = 6;        static void Main(string[] args)
            {
                Items = "01723240".ToCharArray();
                Array.Sort(Items);
                PrintOut(0, TLength);
                Console.ReadKey();
            }        private static void PrintOut(int currentIndex, int length)
            {
                if (length == 0)
                {
                    Console.WriteLine(new string(Items, 0, TLength));
                    return;
                }            char currentValue = (char)0;            for (int i = currentIndex; i < Items.Length; i++)
                {
                    if (currentValue != Items[i])
                    {
                        SwapItem(currentIndex, i);
                        PrintOut(currentIndex + 1, length - 1);
                        SwapItem(i, currentIndex);
                        currentValue = Items[i];
                    }
                }
            }        private static void SwapItem(int indexA, int indexB)
            {
                int m = indexA > indexB ? 1 : -1;
                char value = Items[indexB];            while (indexA != indexB)
                {
                    Items[indexB] = Items[indexB + m];
                    indexB += m;
                }            Items[indexA] = value;
            }
        }
    }
      

  6.   

    http://blog.csdn.net/LCL_data/archive/2010/02/03/5286847.aspx
      

  7.   

    非常感谢 lzsh0622 的代码 虽然还有重复 但已经很容易排除 真的很棒 学习了同时感谢大家支持 问题已经解决