本文主要是介绍如何使用ArrayList类实现n位数的任意组合?也考虑0在首位的情况,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
using System;using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
#region @吴新强于2013年4月10日21:00:46 桂电2507
#endregion
namespace ArbitrarinessCombination
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
static string[] a = new string[10] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
int num=10;
int i = 0, j = 0;
/// <summary>
/// 自定义函数
/// </summary>
/// <param name="n"></param>
/// <param name="List"></param>
public void SetArbitrariness(int n, ArrayList List)
{
ArrayList SList = new ArrayList(a);
SList.Clear();
try
{
if (n >= 1)
{
if (List.Count == 0)
{
for (i = 0; i <= num - 1; i++)
SList.Add(a[i]);
}
else
{
for (i = 0; i <= num - 1; i++)
for (j = 0; j <= List.Count - 1; j++)
if (List[j].ToString().IndexOf(a[i]) == -1)
SList.Add(a[i] + List[j].ToString());
}
SetArbitrariness(n - 1, SList);
}
if (SList.Count > 0)
{
List.Clear();
for (i = 0; i < SList.Count; i++)
List.Add(SList[i].ToString());
}
}
catch
{
SList.Clear();
}
}
/// <summary>
/// 计算按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
ArrayList List = new ArrayList(a);
List.Clear();
listBox1.Items.Clear();
SetArbitrariness(Convert.ToInt32(textBox1.Text), List);
for (i = 0; i < List.Count; i++)
listBox1.Items.Add(List[i].ToString());
listBox1.Items.Add("Total:" + listBox1.Items.Count.ToString());
}
}
}
这篇关于如何使用ArrayList类实现n位数的任意组合?也考虑0在首位的情况的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!