-2

可能重复:
如何获得子集的所有可能组合?

我正在尝试键入给定字符串的集合,例如“123”将给出 {1}{2}{3}{13}{23}{12}{123}{} 但我的代码给了我 1 1 请可以任何人告诉我为什么,请帮我解决它谢谢大家

using System;
using System.Collections.Generic;
using System.Linq; 
using System.Text;

namespace TestAAD
{
class Program
{
    static List<string> sets = new List<string>();
    static int len = 0;

    private static void Generte_Sets(string str, int i)
    {

        sets.Add(str[i].ToString());

        if (i < len)
            Generte_Sets(str, i + 1);
        else
        {
            for (int j = 0; j < sets.Count; j++)
                Console.Write(sets[j]);
            Console.WriteLine();
        }

        sets.Remove(str[i].ToString());  
        if (i < len)
            Generte_Sets(str, i + 1);
        else
        {
            for (int j = 0; j < sets.Count; j++)
                Console.Write(sets[j]);
            Console.WriteLine();
        }
    }

    static void Main(string[] args)
    {
        string set = "123";

        Generte_Sets(set, 0);
        len = set.Length;
        for (int i = 0; i < sets.Count; i++)
        {
            Console.WriteLine(sets[i]);
        }
    }
}

}

请我需要帮助来输入我需要快速帮助的集合 谢谢大家

4

1 回答 1

0
class Program
    {
        static List<string> sets = new List<string>();
        static int len = 0;

        private static void Generte_Sets(string str, int i)
        {
            sets.Add(str[i].ToString());           

            **if (i + 1 < len)**
                Generte_Sets(str, i + 1);
            else
            {
                for (int j = 0; j < sets.Count; j++)
                    Console.Write(sets[j]);
                Console.WriteLine();
            }

            sets.Remove(str[i].ToString());          

            **if (i + 1 < len)**
                Generte_Sets(str, i + 1);
            else
            {
                for (int j = 0; j < sets.Count; j++)
                    Console.Write(sets[j]);
                Console.WriteLine();
            }
        }

        static void Main(string[] args)
        {
             string set = "123"; 
             **len = set.Length;**
             Generte_Sets(set, 0);

            for (int i = 0; i < sets.Count; i++)
            {
                Console.WriteLine(sets[i]);
            }
        }
    }
于 2012-12-08T15:50:37.463 回答