平均分配算法

来源:互联网 发布:大数据培训考试 编辑:程序博客网 时间:2024/05/08 13:29
  1. http://blog.csdn.net/CutBug/archive/2009/01/27/3853648.aspx
  2. using System;   
  3. using System.Collections.Generic;   
  4. namespace Test   
  5. {   
  6.     class Program    
  7.     {   
  8.         static List<int> listCus = new List<int>();   
  9.         static List<string> peasons = new List<string>();   
  10.         static Dictionary<string, List<int>> result = new Dictionary<string, List<int>>();   
  11.         static void Main(string[] args)   
  12.         {  
  13.             #region 初始化数据   
  14.             int m = 4230;   
  15.             int n = 50;   
  16.             for (int i = 0; i < m; i++)   
  17.             {   
  18.                 listCus.Add(i);   
  19.                    
  20.             }   
  21.             Console.WriteLine("分配结果:");   
  22.             for (int j = 0; j < n; j++)   
  23.             {   
  24.                 string t = (j+1).ToString().PadLeft(12, '0');   
  25.                 peasons.Add(t);   
  26.                 result.Add(t, new List<int>());   
  27.             }  
  28.             #endregion   
  29.  
  30.             #region 算法部分   
  31.             int iAv = m / n;   
  32.             int iMod = m % n;   
  33.             int it = 0 ;   
  34.             int ll = 0 ;   
  35.             foreach (string k in result.Keys)   
  36.             {   
  37.                 ll = iMod > 0 ? 1 : 0;   
  38.                 result[k].AddRange(listCus.GetRange(it, iAv + ll));   
  39.                 it += iAv;   
  40.                 --iMod;   
  41.                 Console.WriteLine("{0}:{1}",k,result[k].Count);   
  42.             }  
  43.             #endregion    
  44.   
  45.         }   
  46.     }   
  47.        
  48. }  
原创粉丝点击