双色球33个数全排
static void Main(string[] args){
DateTime startTime = DateTime.Now;
int a, b, c, d, e, f;
for (a = 1; a < 29; a++)
{
for (b = 1; b < 30; b++)
{
for (c = 1; c < 31; c++)
{
for (d = 1; d < 32; d++)
{
for (e = 1; e < 33; e++)
{
for (f = 1; f < 34; f++)
{
Console.WriteLine(a + "," + b + "," + c + "," + d + "," + e + "," + f );
}
}
}
}
}
}
double secondes = DateTime.Now.Subtract(startTime).TotalSeconds;
Console.WriteLine();
Console.WriteLine(secondes);
Console.ReadKey();
}
就是把1-33这些数 6个一组全排列,要求没有重付注。
这个方法算,时间太长了。我看到别人的软件,不到一秒就算过了。不知道用的是什么方法。
--------------------编程问答-------------------- 先算出来,放到数据文件里面 --------------------编程问答-------------------- 同上,空间换时间,把全排列的结果保存起来,之后消耗的只是读文件的时间。
算法上,我这里有一个排列组合类,可供参考。组合类是我自己写的,排列类是网上抓来的。
namespace PermutationAndCombination--------------------编程问答-------------------- --------------------编程问答-------------------- --------------------编程问答-------------------- up --------------------编程问答-------------------- up --------------------编程问答-------------------- 同志们好啊!
{
public class CombinationGenerator : IEnumerable<int[]>
{
private int _m;
private int _n;
public int m { get { return _m; } }
public int n { get { return _n; } }
public CombinationGenerator(int m, int n)
{
if (m < n)
throw new ArgumentException("m must be larger or equal than n.", "m");
_m = m;
_n = n;
}
#region IEnumerable<int[]> Members
public IEnumerator<int[]> GetEnumerator()
{
if (m == n)
{
int[] result = new int[this.n];
for (int i = 0; i < n; i++)
{
result[i] = i;
}
yield return result;
}
else
{
//build a combination array
int count = 0;//C(m,n),row of the array
int u = 1; int d = 1;
for (int i = n + 1; i <= this.m; i++) u *= i;
for (int i = 1; i <= this.m - this.n; i++) d *= i;
count = u / d;
bool[,] result = new bool[count, this.m];
int lastColumn = 0;
for (int r = 0; r < count; r++) for (int c = 0; c < this.m; c++) result[r, c] = false;
for (int i = 0; i < this.n; i++) result[0, i] = true;
for (int r = 0; r < count; r++)
{
if (r == 0)
{
for (int c = 0; c < this.n; c++) result[0, c] = true;
}
else
{
for (int c = 0; c < this.m; c++)
result[r, c] = result[r - 1, c];
//swap the first 'true-false' to 'false-true' in this row.
for (int c = 0; c < this.m - 1; c++)
{
if (result[r, c] && !result[r, c + 1])
{
result[r, c] = false;
result[r, c + 1] = true;
lastColumn = c;
break;
}
}
int trueCount = 0;
//move all true's to the left side of this row.
for (int c = 0; c < lastColumn; c++) if (result[r, c]) trueCount++;
if (trueCount < lastColumn)
{
for (int c = 0; c < trueCount; c++) result[r, c] = true;
for (int c = trueCount; c < lastColumn; c++) result[r, c] = false;
}
}
int[] columnResult = new int[this.n];
int p = 0;
for (int c = 0; c < this.m; c++)
{
if (result[r, c])
columnResult[p++] = c;
}
yield return columnResult;
}
}
}
#endregion
#region IEnumerable Members
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
#endregion
}
public class PermutationGenerator : IEnumerable<int[]>
{
private int total;
public int Total { get { return total; } }
public IEnumerator<int[]> GetEnumerator()
{
if (total == 1)
yield return new int[1] { 0 };
else
{
//########################
// ############## Stack Info
int stackidx = 0;
int stkSize = total * total / 2;
int[] stack_num = new int[stkSize];
int[] stack_level = new int[stkSize];
//########################
int totalminus1 = total - 1;
int level = 0;
int curpos = -1;
int lastSetLevel = -1;
//
bool[] mSelected = new bool[total];
int[] mResult = new int[total];
for (int i = totalminus1; i >= 0; i--)
{
// Initail Stack , Push
stack_num[stackidx] = i;
stack_level[stackidx] = level;
stackidx++;
}
// While Stack not empty
while (stackidx != 0)
{
// ################
// ########### Pop
stackidx--;
curpos = stack_num[stackidx];
level = stack_level[stackidx];
// ################
for (int i = level; i <= lastSetLevel; i++)
{
mSelected[mResult[i]] = false;
}
mSelected[curpos] = true;
mResult[level] = curpos;
lastSetLevel = level;
if (level == totalminus1)
{
yield return mResult;
}
else
{
if (level < totalminus1)
{
for (int i = totalminus1; i >= 0; i--)
{
if (mSelected[i]) continue;
// ################
// ########### Push
stack_num[stackidx] = i;
stack_level[stackidx] = level + 1;
stackidx++;
}
}
}
}
}
}
public PermutationGenerator(int total)
{
if (total < 0) throw new Exception("[TOTAL] can not be less than [ZERO]");
this.total = total;
}
#region IEnumerable Members
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
#endregion
}
}
补充:.NET技术 , C#