1. 程式人生 > >不固定數量 按比例分配(將比例轉為權重)

不固定數量 按比例分配(將比例轉為權重)

math clas code comm 最大公約數 toa ice 數據 後臺

遇到的問題:後臺給多個不同管道設置比例,根據遞增的數據和後臺設置的比例尋找應用管道

 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();
         
        }

不固定數量 按比例分配(將比例轉為權重)