首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

求高效的组合算法,该怎么处理

2012-01-23 
求高效的组合算法C# codeprivate void Combining(string str,int count)//str中没有重复字符,要求将str中

求高效的组合算法

C# code
private void Combining(string str,int count)//str中没有重复字符,要求将str中的字符任意提取出count个进行组合。不用排列。str的长度大于等于count。{if(str.Length<count) return;//这里的代码如何写?}


[解决办法]
说简单点获取能写..目前没弄明白..
 
[解决办法]
private void Combining(string str,int count)
//str中没有重复字符,要求将str中的字符任意提取出count个进行组合。不用排列。str的长度大于等于count。
{
if(str.Length<count) return;
//这里的代码如何写?
}
先改成下面的好吗?
private string[] Combining(string str,int count)
//str中没有重复字符,要求将str中的字符任意提取出count个进行组合。不用排列。str的长度大于等于count。
{
if(str.Length<count) return;
//这里的代码如何写?
}

[解决办法]
按LZ的要求,测试通过

C# code
class Program{    static void Main(string[] args)    {        Combining("58fgj6nsz",5);    }    static void Combining(string str, int count)    //str中没有重复字符,要求将str中的字符任意提取出count个进行组合。不用排列。str的长度大于等于count。    {        string res = "";        if (str.Length < count) return;        Random r = new Random();        while (res.Length != count)        {            string t = str[r.Next(str.Length)].ToString();            if (!res.Contains(t.ToString()))                res += t;        }        Console.WriteLine(res);    }}
[解决办法]
任意提取出count个进行组合。不用排列。str的长度大于等于count
好像题意有问题


string str="ABCDEFG"; 
int count=3;

如果是这样,符合的组合是不是也包括,ABCD,ABCDE...等等呢
你前面有说“str的长度大于等于count”
[解决办法]
楼上的只取出来一种组合
需要多一次循环...
[解决办法]
顶。。。
[解决办法]
用随机数吗?
[解决办法]
C# code
namespace WindowsApplication9{    public partial class Form1 : Form    {        String Result = String.Empty;        public Form1()        {            String S = "ABCDEFGH";            int Count = 3;            String TempResult = String.Empty;            GetIt(S, Count, ref TempResult);            MessageBox.Show(Result);        }        void GetIt(String S, int Count, ref String TempResult)        {            if (Count == 0)            {                Result += TempResult + " ";                return;            }            for (int i = 0; i < S.Length; i++)            {                String C = S.Substring(i, 1);                String T = S.Remove(i, 1);                TempResult += C;                GetIt(T, Count - 1, ref TempResult);                TempResult = TempResult.Remove(TempResult.Length - 1, 1);            }        }    }}
[解决办法]
LS的,只组合,不排列;囧
[解决办法]
探讨
LS的,只组合,不排列;囧

热点排行