protected void Button1_Click(object sender, EventArgs e) { ArrayList list = new ArrayList(); Random random = new Random(); list.Add(random.Next(1, 33)); int count = 1; for (int i = 1; i < 100; i++) { int a = random.Next(1, 33); if (!list.Contains(a)) { list.Add(a); count++; } if (count == 6) { break; } } int b = random.Next(1, 16); list.Add(b); Response.Write("红球:"); for (int i = 0; i < list.Count; i++) { if (i < 6) { Response.Write(list[i] + " ,"); } else { Response.Write("蓝球:" + list[i] + " "); } } } 红球1到33 蓝球1到16 红6蓝1一组
using System; using System.Collections.Generic; using System.Text; using System.Text.RegularExpressions;namespace CSharpConsoleTest { class Program { static void Main(string[] args) { List<string> list = new List<string>(); list.Add("A"); list.Add("B"); list.Add("C"); foreach (List<string> item in C(list, 1)) { A(item, 0, item.Count-1); } Console.WriteLine("---------------------"); foreach (List<string> item in C(list, 2)) { A(item, 0, item.Count - 1); } Console.WriteLine("---------------------"); foreach (List<string> item in C(list, 3)) { A(item, 0, item.Count - 1); } Console.WriteLine("---------------------"); } /// <summary> /// 对数组进行组合操作,选取selectCount个元素进行组合 /// </summary> /// <param name="lsArray">即将进行组合操作的数组</param> /// <param name="selectCount">选取的元素的个数</param> static List<List<string>> C(List<string> lsArray, int selectCount) { List<List<string>> ll = new List<List<string>>(); int totolcount = lsArray.Count; int[] currectselect = new int[selectCount]; int last = selectCount - 1; for (int i = 0; i < selectCount; i++) { currectselect[i] = i; } while (true) { List<string> list = new List<string>(); for (int i = 0; i < selectCount; i++) { // Console.Write(" {0} ", lsArray[currectselect[i]]); list.Add(lsArray[currectselect[i]].ToString()); } ll.Add(list); if (currectselect[last] < totolcount - 1) { currectselect[last]++; } else { int pos = last; while (pos > 0 && currectselect[pos - 1] == currectselect[pos] - 1) { pos--; } if (pos == 0) return ll; currectselect[pos - 1]++; for (int i = pos; i < selectCount; i++) { currectselect[i] = currectselect[i - 1] + 1; } } } } /// <summary> /// 对数组进行全排列 /// </summary> /// <param name="lsArray">要进行全排列的数组</param> /// <param name="begin">进行全排列的开始下标</param> /// <param name="end">进行全排列的结束下标</param> static void A(List<string> lsArray, int begin, int end) { if (begin == end) { for (int i = 0; i <= end; i++) Console.Write(" {0} ", lsArray[i]); Console.WriteLine(); } for (int i = begin; i <= end; i++) { Swap(lsArray, begin, i); A(lsArray, begin + 1, end); Swap(lsArray, begin, i); } } /// <summary> /// 交换数组中的下标为x,y的值 /// </summary> /// <param name="lsArray">该数组</param> /// <param name="x"></param> /// <param name="y"></param> static void Swap(List<string> lsArray, int x, int y) { string t = lsArray[x]; lsArray[x] = lsArray[y]; lsArray[y] = t; } } } A B C --------------------- A B B A A C C A B C C B --------------------- A B C A C B B A C B C A C B A C A B --------------------- 请按任意键继续. . .
using System; using System.Collections.Generic; namespace Test { class Program { static void Main(string[] args) { Console.WriteLine(P1(6, 3)); Console.WriteLine(P2(6, 3)); Console.WriteLine(C(6, 2)); }
/// <summary> /// 排列循环方法 /// </summary> /// <param name="N"></param> /// <param name="R"></param> /// <returns></returns> static long P1(int N, int R) { if (R > N || R <= 0 || N <= 0 ) throw new ArgumentException("params invalid!"); long t = 1; int i = N;
while (i!=N-R) { try { checked { t *= i; } } catch { throw new OverflowException("overflow happens!"); } --i; } return t; }
/// <summary> /// 排列堆栈方法 /// </summary> /// <param name="N"></param> /// <param name="R"></param> /// <returns></returns> static long P2(int N, int R) { if (R > N || R <= 0 || N <= 0 ) throw new ArgumentException("arguments invalid!"); Stack<int> s = new Stack<int>(); long iRlt = 1; int t; s.Push(N); while ((t = s.Peek()) != N - R) { try { checked { iRlt *= t; } } catch { throw new OverflowException("overflow happens!"); } s.Pop(); s.Push(t - 1); } return iRlt; }
/// <summary> /// 组合 /// </summary> /// <param name="N"></param> /// <param name="R"></param> /// <returns></returns> static long C(int N, int R) { return P1(N, R) / P1(R, R); } }
{
ArrayList list = new ArrayList();
Random random = new Random();
list.Add(random.Next(1, 33));
int count = 1;
for (int i = 1; i < 100; i++)
{
int a = random.Next(1, 33);
if (!list.Contains(a))
{
list.Add(a);
count++;
}
if (count == 6)
{
break;
}
} int b = random.Next(1, 16);
list.Add(b);
Response.Write("红球:");
for (int i = 0; i < list.Count; i++)
{ if (i < 6)
{
Response.Write(list[i] + " ,");
}
else
{
Response.Write("蓝球:" + list[i] + " ");
}
} }
红球1到33 蓝球1到16 红6蓝1一组
不知道单个词组内字符能否重复?
不能重复则是排列问题求解,随便找本算法书都有的
能重复的话你可以把它看做n进制数转为字符串的算
法来解.比如二进制的字符集是0和1,16进制的字符
集为0~F.对于一个整数x转成n进制的字符串也是唯
一的.
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;namespace CSharpConsoleTest
{
class Program
{
static void Main(string[] args)
{
List<string> list = new List<string>();
list.Add("A");
list.Add("B");
list.Add("C"); foreach (List<string> item in C(list, 1))
{
A(item, 0, item.Count-1);
}
Console.WriteLine("---------------------");
foreach (List<string> item in C(list, 2))
{
A(item, 0, item.Count - 1);
}
Console.WriteLine("---------------------");
foreach (List<string> item in C(list, 3))
{
A(item, 0, item.Count - 1);
}
Console.WriteLine("---------------------"); } /// <summary>
/// 对数组进行组合操作,选取selectCount个元素进行组合
/// </summary>
/// <param name="lsArray">即将进行组合操作的数组</param>
/// <param name="selectCount">选取的元素的个数</param>
static List<List<string>> C(List<string> lsArray, int selectCount)
{
List<List<string>> ll = new List<List<string>>();
int totolcount = lsArray.Count;
int[] currectselect = new int[selectCount];
int last = selectCount - 1; for (int i = 0; i < selectCount; i++)
{
currectselect[i] = i;
} while (true)
{
List<string> list = new List<string>();
for (int i = 0; i < selectCount; i++)
{
// Console.Write(" {0} ", lsArray[currectselect[i]]);
list.Add(lsArray[currectselect[i]].ToString());
} ll.Add(list); if (currectselect[last] < totolcount - 1)
{
currectselect[last]++;
}
else
{
int pos = last;
while (pos > 0 && currectselect[pos - 1] == currectselect[pos] - 1)
{
pos--;
}
if (pos == 0) return ll;
currectselect[pos - 1]++;
for (int i = pos; i < selectCount; i++)
{
currectselect[i] = currectselect[i - 1] + 1;
}
}
}
} /// <summary>
/// 对数组进行全排列
/// </summary>
/// <param name="lsArray">要进行全排列的数组</param>
/// <param name="begin">进行全排列的开始下标</param>
/// <param name="end">进行全排列的结束下标</param>
static void A(List<string> lsArray, int begin, int end)
{
if (begin == end)
{
for (int i = 0; i <= end; i++)
Console.Write(" {0} ", lsArray[i]);
Console.WriteLine();
}
for (int i = begin; i <= end; i++)
{
Swap(lsArray, begin, i);
A(lsArray, begin + 1, end);
Swap(lsArray, begin, i);
}
} /// <summary>
/// 交换数组中的下标为x,y的值
/// </summary>
/// <param name="lsArray">该数组</param>
/// <param name="x"></param>
/// <param name="y"></param>
static void Swap(List<string> lsArray, int x, int y)
{
string t = lsArray[x];
lsArray[x] = lsArray[y];
lsArray[y] = t;
}
}
}
A
B
C
---------------------
A B
B A
A C
C A
B C
C B
---------------------
A B C
A C B
B A C
B C A
C B A
C A B
---------------------
请按任意键继续. . .
using System.Collections.Generic;
namespace Test
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(P1(6, 3));
Console.WriteLine(P2(6, 3));
Console.WriteLine(C(6, 2));
}
/// <summary>
/// 排列循环方法
/// </summary>
/// <param name="N"></param>
/// <param name="R"></param>
/// <returns></returns>
static long P1(int N, int R)
{
if (R > N || R <= 0 || N <= 0 ) throw new ArgumentException("params invalid!");
long t = 1;
int i = N;
while (i!=N-R)
{
try
{
checked
{
t *= i;
}
}
catch
{
throw new OverflowException("overflow happens!");
}
--i;
}
return t;
}
/// <summary>
/// 排列堆栈方法
/// </summary>
/// <param name="N"></param>
/// <param name="R"></param>
/// <returns></returns>
static long P2(int N, int R)
{
if (R > N || R <= 0 || N <= 0 ) throw new ArgumentException("arguments invalid!");
Stack<int> s = new Stack<int>();
long iRlt = 1;
int t;
s.Push(N);
while ((t = s.Peek()) != N - R)
{
try
{
checked
{
iRlt *= t;
}
}
catch
{
throw new OverflowException("overflow happens!");
}
s.Pop();
s.Push(t - 1);
}
return iRlt;
}
/// <summary>
/// 组合
/// </summary>
/// <param name="N"></param>
/// <param name="R"></param>
/// <returns></returns>
static long C(int N, int R)
{
return P1(N, R) / P1(R, R);
}
}
}
http://blog.csdn.net/CutBug/archive/2009/01/27/3853665.aspx