如何键入设置给定的字符串 [英] How to type sets for a given string

查看:159
本文介绍了如何键入设置给定的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:结果
我怎样才能获得一个子集的所有可能的组合?






我想键入集例如
中的字符串123将会给{1} {2} {3} {13} {23 } {12} {123} {}
,但我的代码给了我1
请谁能告诉我为什么,并请帮我解决这个问题
感谢所有

 使用系统; 
使用System.Collections.Generic;
使用System.Linq的;
使用System.Text;

命名空间TestAAD
{
类节目
{
静态列表<串GT;套=新的List<串GT;();
静态INT LEN = 0;

私有静态无效Generte_Sets(字符串str,int i)以
{

sets.Add(STR [I]的ToString());

如果(ⅰ&下; LEN)
Generte_Sets(STR,I + 1);
,否则
{
为(INT J = 0; J< sets.Count; J ++)
Console.Write(套[J]);
Console.WriteLine();
}

sets.Remove(STR [I]的ToString());
如果(ⅰ&下; LEN)
Generte_Sets(STR,I + 1);
,否则
{
为(INT J = 0; J< sets.Count; J ++)
Console.Write(套[J]);
Console.WriteLine();
}
}

静态无效的主要(字串[] args)
{
字符串设置=123;

Generte_Sets(集,0);
LEN = set.Length;
的for(int i = 0; I< sets.Count;我++)
{
Console.WriteLine(套[I]);
}
}
}



}



请我需要帮助键入套
我需要快速帮助
感谢所有


解决方案

 类节目
{
静态列表<串GT;套=新的List<串GT;();
静态INT LEN = 0;

私有静态无效Generte_Sets(字符串str,int i)以
{
sets.Add(STR [I]的ToString());

**如果第(i + 1&下; LEN)**
Generte_Sets(STR,I + 1);
,否则
{
为(INT J = 0; J< sets.Count; J ++)
Console.Write(套[J]);
Console.WriteLine();
}

sets.Remove(STR [I]的ToString());

**如果第(i + 1&下; LEN)**
Generte_Sets(STR,I + 1);
,否则
{
为(INT J = 0; J< sets.Count; J ++)
Console.Write(套[J]);
Console.WriteLine();
}
}

静态无效的主要(字串[] args)
{
字符串设置=123;
** LEN = set.Length; **
Generte_Sets(集,0);

的for(int i = 0; I< sets.Count;我++)
{
Console.WriteLine(套[I]);
}
}
}


Possible Duplicate:
How can I obtain all the possible combination of a subset?

I am trying to type the sets for a given string for example "123" will give {1}{2}{3}{13}{23}{12}{123}{} but my code gives me 1 1 Please can Anyone tell me why and please help me to fix it Thanks all

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]);
        }
    }
}

}

Please I need help to type the sets I need quick help thanks All

解决方案

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]);
            }
        }
    }

这篇关于如何键入设置给定的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆