using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int totalMass = Convert.ToInt32(this.textBox2.Text);
int totalFiles = Convert.ToInt32(this.textBox1.Text);
int[] Arr = new int[totalFiles];
Random rnd = new Random();
//随便产生些大小不一的文件
for (int i = 0; i < totalFiles; i++)
{
Arr[i] = rnd.Next(100);
}
this.listBox1.DataSource = Arr;
int finalMass = 0;
int mass = 0;
int a = 0;
int b = 0;
string finalResult = "";
string result = "";
richTextBox1.Text = "正在计算,请梢侯...";
pictureBox1.Show();
DateTime T = DateTime.Now;
//开始结算
for (int x = 1; x < Math.Pow(2, totalFiles); x++)
{
result = "";
mass = 0;
b = 0;
a = x;
while (a != 0)
{
if (a % 2 != 0)
{
result += (b+1) + "-[" + Arr[b] + "],";
mass += Arr[b];
}
b++;
a >>= 1;
Application.DoEvents();
if (mass > finalMass && mass < 700)
{
finalMass = mass;
finalResult = result;
}
}
Application.DoEvents();
}
richTextBox1.Text = "总计:" + finalMass + "兆,用时:" + (DateTime.Now-T).ToString();
richTextBox1.Text +="\n选择:\n" + finalResult.Substring(0, finalResult.Length - 1);
pictureBox1.Hide();
}