不固定数量 按比例分配(将比例转为权重)

遇到的问题:后台给多个不同管道设置比例,根据递增的数据和后台设置的比例寻找应用管道

 public class RationChoose
    {
        /// <summary>
        /// 使用的选择
        /// </summary>
        private int currentIndex = -1;

        /// <summary>
        /// 使用的权重
        /// </summary>
        private int currentWeight = 0;

        /// <summary>
        /// 权重的最大公约数
        /// </summary>
        private int gcdWeight;

        /// <summary>
        /// 权重总和
        /// </summary>
        private int sumWeight;

        /// <summary>
        /// 最大权重
        /// </summary>
        private int maxWeight;

        /// <summary>
        /// 可用比例数
        /// </summary>
        private int rationCount;


        private Dictionary<int, int> rationList;

        //构造
        public RationChoose(Dictionary<int, int> list)
        {
            rationList = list;

            rationCount = rationList.Count;

            var weightArray = rationList.Select(x => x.Value).ToArray();

            gcdWeight = CommonMaths_OperService.GetDivisor(weightArray);

            weightArray = weightArray.Select(x => x / gcdWeight).ToArray();

            maxWeight = weightArray.Max();

            sumWeight = weightArray.Sum();

            gcdWeight = 1;
        }
        
        public int GetRouteInfo(int nums)
        {
            int rationInfo = -1;

            if (1 == rationCount)
            {
                rationInfo = rationList.First().Key;
            }
            else
            {
                if (sumWeight == 0)
                {
                    return -1;
                }

                nums = nums % sumWeight == 0 ? sumWeight : nums % sumWeight;

                int count = 0;
                while (count != nums)
                {
                    count++;
                    rationInfo = GetRouteInfo();
                }
            }
            return rationInfo;
        }
       
        private int GetRouteInfo()
        {
            while (true)
            {
                currentIndex = (currentIndex + 1) % rationCount;
                if (currentIndex == 0)
                {
                    currentWeight = currentWeight - gcdWeight;

                    if (currentWeight <= 0)
                    {
                        currentWeight = maxWeight;

                        if (currentWeight == 0)
                        {
                            return -1;
                        }
                    }
                }

                if (rationList[currentIndex] >= currentWeight)
                {
                    return currentIndex;
                }
            }
        }
    }
 public class CommonMaths_OperService
    {
        /// <summary>
        /// 计算公约数
        /// </summary>
        /// <param name="nums"></param>
        /// <returns></returns>
        public static int GetDivisor(int[] nums)
        {
            int count = nums.Length;
            if (count == 0)
            {
                return -1;
            }
            int divisor = 0;
            for (int i = 0; i < count - 1; i++)
            {
                if (i == 0)
                {
                    divisor = GetDivisor(nums[i], nums[i + 1]);
                }
                else
                {
                    divisor = GetDivisor(divisor, nums[i + 1]);
                }
            }
            return divisor;
        }

        /// <summary>
        /// 最大公约数
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        public static int GetDivisor(int a, int b)
        {
            if (a < b)
            {
                a = a + b;
                b = a - b;
                a = a - b;
            }
            return (a % b == 0) ? b : GetDivisor(a % b, b);
        }

    }
}
 static void Main(string[] args)
        {

            int A = 0, B = 0, C = 0;

            for (int i = 1; i < 1001; i++)
            {
                RationChoose choose = new RationChoose(new Dictionary<int, int>() { { 0, 35 }, { 1, 35 }, { 2, 30 } });

                int a = choose.GetRouteInfo(i);

                Console.WriteLine(string.Format("数量【{0}】,使用权重【{1}】", i, a));


                if (a == 0)
                {
                    A++;
                }
                else if (a == 1)
                {
                    B++;
                }
                else
                {
                    C++;
                }


            }
            Console.WriteLine(string.Format("数量【{0}】,使用权重【A】", A));
            Console.WriteLine(string.Format("数量【{0}】,使用权重【B】", B));
            Console.WriteLine(string.Format("数量【{0}】,使用权重【C】", C));

            Console.ReadKey();
         
        }

 

转载于:https://www.cnblogs.com/szhStudy/p/10368703.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值