如何使用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());
        }
    }

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值