• 大小: 36.65 KB
    文件类型: .rar
    金币: 2
    下载: 1 次
    发布日期: 2024-07-26
  • 语言: C#
  • 标签: C#  算法  组合  背包问题  

资源简介

组合 算法 C#<br><br>递归实现M选N,可以用于线性规划,背包问题求解等.

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace Combine
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public int[] a;
        public string Result;
        private long T1;
        private void button1_Click(object sender EventArgs e)
        {
            
      
            int M = Convert.ToInt32(this.textBox1.Text);
            int N = Convert.ToInt32(this.textBox2.Text);
            a = new int[N+1];           
            a[0]=N;
            Result = ““;
            this.combine(M N);            
            this.richTextBox1.Text = Result;         
           
            
        }
        private void combine(int m int k)
        {
            //计算组合在m里面选k个元素的全部组合情况,添加到字典对象里           
            for (int i = m; i > k; i--)
            {
                a[k] = i;
                if (k > 1)
                {
                    combine(i - 1 k - 1);
                }
                else
                {
                    for (int j = 1; j <= a[0]; j++)
                    {
                        Result += a[j] + ““;
                    }
                    Result += “\r\n“;
                }
            }
        }

        private void combine2(int m int k)
        {
            //计算组合在m里面选k个元素的全部组合情况,添加到字典对象里           
            for (int i = m; i > k; i--)
            {
                a[k] = i;
                if (k > 1)
                {
                    combine(i - 1 k - 1);
                }
                else
                {                    
                    for (int j = 1; j <= a[0]; j++)
                    {
                        Result += a[j] + ““;                       
                    }
                    Result += “\r\n“;
                }                
            }
        }

        private void Form1_Load(object sender EventArgs e)
        {
            //MessageBox.Show(System.DateTime.Now.Ticks.ToString() );
            //T1 = System.DateTime.Now.Ticks;
            //timer1.Start();
            //MessageBox.Show(TimeSpan.FromSeconds(1).Ticks.ToString());
           MessageBox.Show(Encoding.Default.EncodingName);
        }

        private void timer1_Tick(object sender EventArgs e)
        {
            this.richTextBox1.Text += (System.DateTime.Now.Ticks - T1).ToString() + “\r\n“; 
        }
    }
}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件       9728  2007-09-10 18:25  Combine\Combine\bin\Debug\Combine.exe

     文件      24064  2007-09-10 18:25  Combine\Combine\bin\Debug\Combine.pdb

     文件      15704  2007-09-10 18:25  Combine\Combine\bin\Debug\Combine.vshost.exe

     文件        473  2007-02-21 18:51  Combine\Combine\bin\Debug\Combine.vshost.exe.manifest

     文件       3339  2007-08-29 18:28  Combine\Combine\Combine.csproj

     文件       2747  2007-09-10 18:25  Combine\Combine\Form1.cs

     文件       4039  2007-08-31 13:11  Combine\Combine\Form1.Designer.cs

     文件       6008  2007-08-31 13:11  Combine\Combine\Form1.resx

     文件        420  2007-09-10 18:25  Combine\Combine\obj\Debug\Combine.csproj.FileListAbsolute.txt

     文件        847  2007-08-31 13:12  Combine\Combine\obj\Debug\Combine.csproj.GenerateResource.Cache

     文件       9728  2007-09-10 18:25  Combine\Combine\obj\Debug\Combine.exe

     文件        180  2007-08-31 13:12  Combine\Combine\obj\Debug\Combine.Form1.resources

     文件      24064  2007-09-10 18:25  Combine\Combine\obj\Debug\Combine.pdb

     文件        180  2007-08-29 18:28  Combine\Combine\obj\Debug\Combine.Properties.Resources.resources

     文件        479  2007-08-29 18:27  Combine\Combine\Program.cs

     文件       1434  2007-08-29 18:27  Combine\Combine\Properties\AssemblyInfo.cs

     文件       2843  2007-08-29 18:27  Combine\Combine\Properties\Resources.Designer.cs

     文件       5612  2007-08-29 18:27  Combine\Combine\Properties\Resources.resx

     文件       1092  2007-08-29 18:27  Combine\Combine\Properties\Settings.Designer.cs

     文件        249  2007-08-29 18:27  Combine\Combine\Properties\Settings.settings

     文件        921  2007-08-29 18:27  Combine\Combine.sln

    ..A..H.     12288  2007-09-11 12:15  Combine\Combine.suo

     目录          0  2007-08-29 18:27  Combine\Combine\obj\Debug\TempPE

     目录          0  2007-08-29 18:29  Combine\Combine\bin\Debug

     目录          0  2007-09-10 18:25  Combine\Combine\obj\Debug

     目录          0  2007-08-29 18:27  Combine\Combine\bin

     目录          0  2007-08-29 18:27  Combine\Combine\obj

     目录          0  2007-08-29 18:27  Combine\Combine\Properties

     目录          0  2007-09-10 18:25  Combine\Combine

     目录          0  2007-08-29 18:27  Combine

............此处省略3个文件信息

评论

共有 条评论