日期:2014-05-20 浏览次数:20892 次
private void Combining(string str,int count)
//str中没有重复字符,要求将str中的字符任意提取出count个进行组合。不用排列。str的长度大于等于count。
{
if(str.Length<count) return;
//这里的代码如何写?
}
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”
------解决方案--------------------
楼上的只取出来一种组合
需要多一次循环...
------解决方案--------------------
顶。。。
------解决方案--------------------
用随机数吗?
------解决方案--------------------
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的,只组合,不排列;囧
------解决方案--------------------