Recursive enumeration arrangement, combined C # source code

xiaoxiao2021-03-05  37

Approximately a little thing written in VS7 Beta in 2001, it is not a general childish. . . The advantage is to run :), see the code to have a friend on the 9CBS to C #, I don't want to make ugly. (Convert into VS03 project)

Combinatorics.cs code list

Using system;

Using system.collections;

Using system.data;

///

/// Combined Mathematical Function Set

///

Public Class Combinatorics

{

#Region public function

///

// / convert the two-dimensional array array to a data sheet

///

Public Static DataTable TwodemisionInTarrayTodataTable (int [] Source)

{

DataTable DT = New DataTable ();

DataRow DR;

INT I, J;

INT b1 = source.getupperbound (0), b2 = source.getupperbound (1); // Get various dimension lengths of the two-dimensional table

FOR (i = 0; i <= b1; i ) // Create the fields of the data table with the second dimensional length

Dt.columns.add (i.tostring (), System.Type.gettype ("System.Int32"));

For (i = 0; i <= b2; i ) // Return to each return

{

DR = dt.newrow (); // Prepare to insert a new line

For (j = 0; j <= b1; j ) // fill in the secondary element of the return arrangement in the new row

DR [j.tostring ()] = source [j, i]; // Use the order pointer to get the value of the original element

Dt.Rows.Add (DR); // Insert a new line

}

Return DT;

}

///

/// Conversion function

///

Public Static Int Product (int Start, int finish)

{

INT factorial = 1;

For (int i = start; i <= finish; i )

Factorial * = i;

Return Factorial;

}

///

/// step-by-step function

///

Public Static Int Factorial (int N)

{

Return Product (2, n);

}

///

/// Arrange function

///

Public Static Int ArrangeCount (int M, int N)

{

Return Product (n - m 1, n);

}

///

/// Generate a row function

///

Public static int [,] arrange (int M, int N)

{

INT A = arrangecount (m, n); // Since the number of arrays, arrange the first dimension of the array

INT [,] arrange = new int [m, a]; // definition return array

ArrayList E = New ArrayList (); // Setting an element table

For (int i = 0; i

E.Add (i 1);

Arrange (Ref Arrange, E, M, 0, 0);

Return Arrange;

}

///

/// combined number of functions

///

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

}

///

/// Generate a combined table function

///

Public Static Int [,] combination (int M, int N)

{

INT a = combinationcount (m, n); // request the number of arranging, arrange the first dimension of the array

int [] combination = new int [m, a]; // definition return array

ArrayList E = New ArrayList (); // Setting an element table

For (int i = 0; i

E.Add (i 1);

Combination (Ref Combination, E, M, 0, 0);

Return Combination;

}

#ndregion

#Region internal core

///

/// Arrange function

///

/// Return Value

/// An array of elements available

/// The target selected element is

/// Current return value array column coordinate

/// Current return value array row coordinate

Private static void arrange (ref int [,] reslut, ArrayList Elements, Int M, INT X, INT Y)

{

INT SUB = ArrangeCount (M - 1, Elements.count - 1); // Requires the number of forces of the previous sub-arrangement for (INT i = 0; i

{

INT VAL = RemoveandWrite (Elements, I, REF RESLUT, X, Y, SUB);

If (m> 1) // recursive conditions are greater than 1

Arrange (Ref Reslut, Elements, M - 1, X 1, Y);

Elements.insert (i, val); // Restore the elements just deleted

}

}

///

/// combined function

///

/// Return Value

/// An array of elements available

/// The target selected element is

/// Current return value array column coordinate

/// Current return value array row coordinate

Private static void combination (ref int [,] reslut, arraylist elements, int m, int x, int y)

{

ArrayList TmPElements = new arraylist (); // All elements used in this loop will be temporarily stored in this array

INT Elementscount = Elements.count; // Record the number of optional elements

int SU;

For (INT I = ElementScount - 1; I> = m - 1; I - Y = Sub) // From ElementsCount-1 (ie N-1) to M-1 cycle, move each time pointer

{

Sub = combinationcount (m-1, i); / / to see the number of front sub-combination

INT VAL = RemoveandWrite (Elements, 0, Ref Reslut, X, Y, Sub);

TMPELEMENTS.ADD (VAL); // store this optional element to a temporary array, and restore it to the Elements array after the loop is ended.

IF (SUB> 1 || (Elements.count 1 == M && Elements.count> 0)) // Recursive conditions for sub-combiners greater than 1 or optional elements 1 equal to the current target selection element number and Optional elements are greater than 1

Combination (Ref Reslut, Elements, M - 1, X 1, Y);

Elements.IsertRange (0, TMPELEMENTS); // restores the optional elements of the above loop deleted to the optional element array

}

///

/// Returns the optional element value specified by index, and delete it in the array, then start from the Y row in the X column to continuously write SUBCOMB value

///

Private Static Int RemoveandWrite (ArrayList Elements, Int Index, Ref Int ", INT X, INT Y, INT Count)

{

INT VAL = (int) Elements [index];

Elements.Removeat (Index);

For (int i = 0; i

RESLUT [X, Y I] = VAL;

Return Val;

}

#ndregion

}

Test Forms FRMTEST.CS code list:

Using system;

Using system.drawing;

Using system.collections;

Using system.componentmodel;

Using system.windows.forms;

Using system.data;

Namespace Combinatoricslib

{

///

/// Form1 summary description.

///

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 panelrrmsg;

Private system.windows.Forms.NumericUpdown Numn;

///

/// The required designer variable.

///

Private system.componentmodel.Container Components = NULL;

Public fmtest ()

{

//

// Windows Form Designer Support

//

InitializationComponent ();

//

// Todo: Add any constructor code after INITIALIZECOMPONENT call

//

}

///

/// Clean all the resources being used.

///

Protected Override Void Dispose (Bool Disposing)

{

IF (Disposing)

{

IF (Components! = NULL)

{

Components.dispose ();

}

}

Base.dispose (Disposing);

}

#Region Windows Form Designer Generated Code

///

/// Designer supports the required method - do not use the code editor to modify

/// This method is content.

///

Private vidinitiRizeComponent ()

{

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) (). BeginInit ();

(System.comPonentModel.isupportInitialize) (this.Numm). Begininit ();

(System.comPonentModel.isupportInitialize) (this.NUMN)). Begininit ();

(System.comPonentModel.isupportinitialize) (this.PaNelerrmsg)). Begininit ();

THIS.SUSPENDLAYOUT ();

//

// GridShow

//

this.gridshow.alternatingbackbackcolor = 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.parentrolsbackcolor = system.drawing.color.gainsboro;

This.gridshow.parentrowsforecolor = system.drawing.color.midnightBlue;

THIS.GRIDSHOW.READONLY = true;

This.gridshow.selectionbackColor = system.drawing.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 ("Song", 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 ("Song", 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 ("Song", 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";

//

// panelrrmsg

//

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);

}

#ndregion

///

/// The main entry point for the application.

///

[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);

Paneelerrmsg.Text = "Showing ...";

Gridshow.datasource = Combinatorics.twodemisionInTaRrayTodataTable (reslut); PaneLerrmsg.Text = "iDLE";

}

Else

PaneLerrmsg.Text = "Input Number is invalid";

}

}

}

The operation is as shown in the figure:

It feels nothing to talk nonsense, using the DataTable output to facilitate the enumeration of the binding entity value (such as the attack dictionary). The highcomers have laughed.

转载请注明原文地址:https://www.9cbs.com/read-34101.html

New Post(0)