排列组合

本文介绍了一种排列组合算法的实现方式,提供了两个类:XPermutation 和 Permutation。XPermutation 支持数据可以重复的排列组合,而 Permutation 则用于不重复的数据排列。每个类都包括初始化、获取当前组合、进行下一次排列和重置等方法。
    public class XPermutation
    {
        private int _length;
        private int _number;
        private int[] _current;
        private bool _first;

        /// <summary>
        /// 排列组合数据
        /// </summary>
        public int[] Current { get { return _current; } }
        /// <summary>
        /// 数据长度
        /// </summary>
        public int Length { get { return _length; } }
        /// <summary>
        /// 数据个数
        /// </summary>
        public int Number { get { return _number; } }

        /// <summary>
        /// 数据可以重复的排列组合
        /// </summary>
        /// <param name="length">数据长度</param>
        /// <param name="number">数据个数</param>
        public XPermutation(int length,int number)
        {
            _length = length;
            _number = number;
            _current = new int[length];
            Reset();
        }

        public bool Next()
        {
            if (_first)
            {
                _first = false;
                return true;
            }
            for (int i = 0; i < _length; ++i)
            {
                ++_current[i];
                if (_current[i] == _number) _current[i] = 0;
                else break;
            }
            for (int i = 0; i < _length; ++i) if (_current[i] != 0) return true;
            _first = true;
            return false;
        }

        /// <summary>
        /// 重新初始化
        /// </summary>
        public void Reset()
        {
            for (int i = 0; i < _current.Length; ++i) _current[i] = 0;
            _first = true;
        }
    }


    public class Permutation
    {
        private int _length;
        private int[] _current;
        private bool _stop;
        /// <summary>
        /// 排列组合数据
        /// </summary>
        public int[] Current { get { return _current; } }
        /// <summary>
        /// 数据长度
        /// </summary>
        public int Length { get { return _length; } }

        /// <summary>
        /// 排列组合
        /// </summary>
        /// <param name="length">数据长度</param>
        public Permutation(int length)
        {
            _length = length;
            _current = new int[length];
            for (int i = 0; i < _current.Length; ++i) _current[i] = i;
            _stop = true;
        }

        /// <summary>
        /// 重新初始化
        /// </summary>
        public void Reset()
        {
            for (int i = 0; i < _current.Length; ++i) _current[i] = i;
            _stop = true;
        }

        /// <summary>
        /// 获取一个排列组合的数据
        /// </summary>
        /// <returns></returns>
        public bool Next()
        {
            if (_stop)
            {
                _stop = false;
                return true;
            }
            else
            {
                calc(0);
                _stop = false;
                if (_current[0] == _length) return false;
                return true;
            }
        }

        private void calc(int index)
        {
            if (index == _current.Length)
            {
                _stop = true;
                return;
            }
            if ((_current[index]!=-1) && ((index + 1) != _current.Length)) calc(index + 1);
            if (_stop) return;
            for (++_current[index]; _current[index] < _length; ++_current[index])
            {
                int n = 0;
                for (; n < index; ++n) if (_current[n] == _current[index]) break;
                if (n == index) calc(index + 1);
                if (_stop) return;
            }
            if (index != 0) _current[index] = -1;
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值