大約是2001年時候用vs7 beta寫的一點東西了,現在回看起來不是一般的幼稚。。。好處是還能運行:),看到csdn上有朋友要c# 的代碼,我也不揣簡陋,獻丑了。(轉換成vs03的工程了)
combinatorics.cs代碼清單
using system;
using system.collections;
using system.data;
/// <summary>
/// 組合數學函數集
/// </summary>
public class combinatorics
{
#region 公共函數
/// <summary>
/// 把二維整形數組轉換為數據表
/// </summary>
public static datatable twodemisionintarraytodatatable(int[, ]source)
{
datatable dt = new datatable();
datarow dr;
int i, j;
int b1 = source.getupperbound(0), b2 = source.getupperbound(1); //獲取二維表的各維長度
for (i = 0; i <= b1; i ++ ) //以第二維長度創建數據表的各字段
dt.columns.add(i.tostring(), system.type.gettype("system.int32"));
for (i = 0; i <= b2; i ++ ) //對各返回排列循環
{
dr = dt.newrow(); //準備插入新行
for (j = 0; j <= b1; j ++ ) //在新行中逐個填入返回排列的各元素次序
dr[j.tostring()] = source[j, i]; //用序數指針獲取原元素的值
dt.rows.add(dr); //插入新行
}
return dt;
}
/// <summary>
/// 連乘積函數
/// </summary>
public static int product(int start, int finish)
{
int factorial = 1;
for (int i = start; i <= finish; i ++ )
factorial *= i;
return factorial;
}
/// <summary>
/// 階乘函數
/// </summary>
public static int factorial(int n)
{
return product(2, n);
}
/// <summary>
/// 排列數函數
/// </summary>
public static int arrangecount(int m, int n)
{
return product(n - m + 1, n);
}
/// <summary>
/// 生成排列表函數
/// </summary>
public static int[, ]arrange(int m, int n)
{
int a = arrangecount(m, n); //求得排列數,安排返回數組的第一維
int[, ]arrange = new int[m, a]; //定義返回數組
arraylist e = new arraylist(); //設置元素表
for (int i = 0; i < n; i ++ )
e.add(i + 1);
arrange(ref arrange, e, m, 0, 0);
return arrange;
}
/// <summary>
/// 組合數函數
/// </summary>
public static int combinationcount(int m, int n)
{
int a = product(n - m + 1, n), b = product(2, m); //a=n-m+1 * ... * n ; b = m!
return (int) a/b; //c=a/b
}
/// <summary>
/// 生成組合表函數
/// </summary>
public static int[, ]combination(int m, int n)
{
int a = combinationcount(m, n); //求得排列數,安排返回數組的第一維
int[, ]combination = new int[m, a]; //定義返回數組
arraylist e = new arraylist(); //設置元素表
for (int i = 0; i < n; i ++ )
e.add(i + 1);
combination(ref combination, e, m, 0, 0);
return combination;
}
#endregion
#region 內部核心
/// <summary>
/// 排列函數
/// </summary>
/// <param name="reslut">返回值數組</param>
/// <param name="elements">可供選擇的元素數組</param>
/// <param name="m">目標選定元素個數</param>
/// <param name="x">當前返回值數組的列坐標</param>
/// <param name="y">當前返回值數組的行坐標</param>
private static void arrange(ref int[, ]reslut, arraylist elements, int m, int x, int y)
{
int sub = arrangecount(m - 1, elements.count - 1); //求取當前子排列的個數
for (int i = 0; i < elements.count; i++, y += sub) //每個元素均循環一次,每次循環后移動行指針
{
int val = removeandwrite(elements, i, ref reslut, x, y, sub);
if (m > 1) //遞歸條件為子排列數大于1
arrange(ref reslut, elements, m - 1, x + 1, y);
elements.insert(i, val); //恢復剛才刪除的元素
}
}
/// <summary>
/// 組合函數
/// </summary>
/// <param name="reslut">返回值數組</param>
/// <param name="elements">可供選擇的元素數組</param>
/// <param name="m">目標選定元素個數</param>
/// <param name="x">當前返回值數組的列坐標</param>
/// <param name="y">當前返回值數組的行坐標</param>
private static void combination(ref int[, ]reslut, arraylist elements, int m, int x, int y)
{
arraylist tmpelements = new arraylist(); //所有本循環使用的元素都將暫時存放在這個數組
int elementscount = elements.count; //先記錄可選元素個數
int sub;
for (int i = elementscount - 1; i >= m - 1; i--, y += sub) //從elementscount-1(即n-1)到m-1的循環,每次循環后移動行指針
{
sub = combinationcount(m-1,i); //求取當前子組合的個數
int val = removeandwrite(elements, 0, ref reslut, x, y, sub);
tmpelements.add(val); //把這個可選元素存放到臨時數組,循環結束后一并恢復到elements數組中
if (sub > 1 || (elements.count + 1 == m && elements.count > 0)) //遞歸條件為 子組合數大于1 或 可選元素個數+1等于當前目標選擇元素個數且可選元素個數大于1
combination(ref reslut, elements, m - 1, x + 1, y);
}
elements.insertrange(0, tmpelements); //一次性把上述循環刪除的可選元素恢復到可選元素數組中
}
/// <summary>
/// 返回由index指定的可選元素值,并在數組中刪除之,再從y行開始在x列中連續寫入subcomb個值
/// </summary>
private static int removeandwrite(arraylist elements, int index, ref int[, ]reslut, int x, int y, int count)
{
int val = (int) elements[index];
elements.removeat(index);
for (int i = 0; i < count; i ++ )
reslut[x, y + i] = val;
return val;
}
#endregion
}
測試窗體frmtest.cs代碼清單:
using system;
using system.drawing;
using system.collections;
using system.componentmodel;
using system.windows.forms;
using system.data;
namespace combinatoricslib
{
/// <summary>
/// form1 的摘要說明。
/// </summary>
public class frmtest : system.windows.forms.form
{
private system.windows.forms.datagrid gridshow;
private system.windows.forms.numericupdown numm;
private system.windows.forms.button btngo;
private system.windows.forms.domainupdown domainfunction;
private system.windows.forms.statusbar statusbar1;
private system.windows.forms.statusbarpanel panelerrmsg;
private system.windows.forms.numericupdown numn;
/// <summary>
/// 必需的設計器變量。
/// </summary>
private system.componentmodel.container components = null;
public frmtest()
{
//
// windows 窗體設計器支持所必需的
//
initializecomponent();
//
// todo: 在 initializecomponent 調用后添加任何構造函數代碼
//
}
/// <summary>
/// 清理所有正在使用的資源。
/// </summary>
protected override void dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.dispose();
}
}
base.dispose( disposing );
}
#region windows 窗體設計器生成的代碼
/// <summary>
/// 設計器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內容。
/// </summary>
private void initializecomponent()
{
this.gridshow = new system.windows.forms.datagrid();
this.domainfunction = new system.windows.forms.domainupdown();
this.numm = new system.windows.forms.numericupdown();
this.numn = new system.windows.forms.numericupdown();
this.btngo = new system.windows.forms.button();
this.statusbar1 = new system.windows.forms.statusbar();
this.panelerrmsg = new system.windows.forms.statusbarpanel();
((system.componentmodel.isupportinitialize)(this.gridshow)).begininit();
((system.componentmodel.isupportinitialize)(this.numm)).begininit();
((system.componentmodel.isupportinitialize)(this.numn)).begininit();
((system.componentmodel.isupportinitialize)(this.panelerrmsg)).begininit();
this.suspendlayout();
//
// gridshow
//
this.gridshow.alternatingbackcolor = system.drawing.color.lavender;
this.gridshow.backcolor = system.drawing.color.whitesmoke;
this.gridshow.backgroundcolor = system.drawing.color.lightgray;
this.gridshow.borderstyle = system.windows.forms.borderstyle.none;
this.gridshow.captionbackcolor = system.drawing.color.lightsteelblue;
this.gridshow.captionforecolor = system.drawing.color.midnightblue;
this.gridshow.datamember = "";
this.gridshow.flatmode = true;
this.gridshow.font = new system.drawing.font("tahoma", 8f);
this.gridshow.forecolor = system.drawing.color.midnightblue;
this.gridshow.gridlinecolor = system.drawing.color.gainsboro;
this.gridshow.gridlinestyle = system.windows.forms.datagridlinestyle.none;
this.gridshow.headerbackcolor = system.drawing.color.midnightblue;
this.gridshow.headerfont = new system.drawing.font("tahoma", 8f, system.drawing.fontstyle.bold);
this.gridshow.headerforecolor = system.drawing.color.whitesmoke;
this.gridshow.linkcolor = system.drawing.color.teal;
this.gridshow.location = new system.drawing.point(24, 88);
this.gridshow.name = "gridshow";
this.gridshow.parentrowsbackcolor = system.drawing.color.gainsboro;
this.gridshow.parentrowsforecolor = system.drawing.color.midnightblue;
this.gridshow.readonly = true;
this.gridshow.selectionbackcolor = system.drawing.color.cadetblue;
this.gridshow.selectionforecolor = system.drawing.color.whitesmoke;
this.gridshow.size = new system.drawing.size(648, 344);
this.gridshow.tabindex = 0;
//
// domainfunction
//
this.domainfunction.backcolor = system.drawing.systemcolors.info;
this.domainfunction.font = new system.drawing.font("宋體", 36f, system.drawing.fontstyle.regular, system.drawing.graphicsunit.point, ((system.byte)(134)));
this.domainfunction.forecolor = system.drawing.color.teal;
this.domainfunction.items.add("c");
this.domainfunction.items.add("a");
this.domainfunction.location = new system.drawing.point(24, 8);
this.domainfunction.name = "domainfunction";
this.domainfunction.readonly = true;
this.domainfunction.size = new system.drawing.size(48, 62);
this.domainfunction.tabindex = 1;
this.domainfunction.text = "c";
//
// numm
//
this.numm.backcolor = system.drawing.color.peachpuff;
this.numm.font = new system.drawing.font("宋體", 12f, system.drawing.fontstyle.regular, system.drawing.graphicsunit.point, ((system.byte)(134)));
this.numm.forecolor = system.drawing.color.teal;
this.numm.location = new system.drawing.point(80, 8);
this.numm.maximum = new system.decimal(new int[] {
20,
0,
0,
0});
this.numm.minimum = new system.decimal(new int[] {
1,
0,
0,
0});
this.numm.name = "numm";
this.numm.size = new system.drawing.size(56, 26);
this.numm.tabindex = 4;
this.numm.value = new system.decimal(new int[] {
2,
0,
0,
0});
//
// numn
//
this.numn.backcolor = system.drawing.color.bisque;
this.numn.font = new system.drawing.font("宋體", 12f);
this.numn.forecolor = system.drawing.color.teal;
this.numn.location = new system.drawing.point(80, 40);
this.numn.maximum = new system.decimal(new int[] {
25,
0,
0,
0});
this.numn.minimum = new system.decimal(new int[] {
1,
0,
0,
0});
this.numn.name = "numn";
this.numn.size = new system.drawing.size(56, 26);
this.numn.tabindex = 5;
this.numn.value = new system.decimal(new int[] {
4,
0,
0,
0});
//
// btngo
//
this.btngo.backcolor = system.drawing.color.paleturquoise;
this.btngo.location = new system.drawing.point(184, 24);
this.btngo.name = "btngo";
this.btngo.size = new system.drawing.size(88, 32);
this.btngo.tabindex = 6;
this.btngo.text = "go!";
this.btngo.click += new system.eventhandler(this.btngo_click);
//
// statusbar1
//
this.statusbar1.location = new system.drawing.point(0, 453);
this.statusbar1.name = "statusbar1";
this.statusbar1.panels.addrange(new system.windows.forms.statusbarpanel[] {
this.panelerrmsg});
this.statusbar1.showpanels = true;
this.statusbar1.size = new system.drawing.size(704, 32);
this.statusbar1.tabindex = 7;
this.statusbar1.text = "statusbar1";
//
// panelerrmsg
//
this.panelerrmsg.autosize = system.windows.forms.statusbarpanelautosize.contents;
this.panelerrmsg.text = "idle";
this.panelerrmsg.width = 39;
//
// frmtest
//
this.autoscalebasesize = new system.drawing.size(6, 14);
this.clientsize = new system.drawing.size(704, 485);
this.controls.add(this.statusbar1);
this.controls.add(this.btngo);
this.controls.add(this.numn);
this.controls.add(this.numm);
this.controls.add(this.domainfunction);
this.controls.add(this.gridshow);
this.maximizebox = false;
this.name = "frmtest";
this.startposition = system.windows.forms.formstartposition.centerscreen;
this.text = "frmtest";
((system.componentmodel.isupportinitialize)(this.gridshow)).endinit();
((system.componentmodel.isupportinitialize)(this.numm)).endinit();
((system.componentmodel.isupportinitialize)(this.numn)).endinit();
((system.componentmodel.isupportinitialize)(this.panelerrmsg)).endinit();
this.resumelayout(false);
}
#endregion
/// <summary>
/// 應用程序的主入口點。
/// </summary>
[stathread]
static void main()
{
application.run(new frmtest());
}
private void btngo_click(object sender, system.eventargs e)
{
int[, ]reslut;
int m = (int)numm.value, n = (int)numn.value;
if (m <= n)
{
panelerrmsg.text = "running...";
if (domainfunction.text == "a")
reslut = combinatorics.arrange(m, n);
else
reslut = combinatorics.combination(m, n);
panelerrmsg.text = "showing...";
gridshow.datasource = combinatorics.twodemisionintarraytodatatable(reslut);
panelerrmsg.text = "idle";
}
else
panelerrmsg.text = "input number is invalid";
}
}
}
運行效果如圖:
感覺沒什么好廢話的,用datatable輸出就是為了方便綁定實體值(比如做攻擊字典)的枚舉。各路高人見笑了。