天天看点

将任意集合, 平分成若干份算法, 有待优化

public List<List<T>> AverageSplit<T>(ref List<T> source, int groupCount)
		{
			List<List<T>> list = new List<List<T>>();

			if (groupCount == 0 || source == null)
				return list;

			if (source.Count < groupCount)
			{
				list.Add(source);
				return list;
			}

			int currentIndex = 0, z = 1;
			int y = source.Count % groupCount;
			int g = source.Count / groupCount;
			for (int i = 1; i <= groupCount; i++)
			{
				if (z > y)
				{
					List<T> fb = source.GetRange(currentIndex, g);
					currentIndex = g * i;
					list.Add(fb);
				}
				else if (z <= y)
				{
					List<T> fb = source.GetRange(currentIndex, g + 1);
					list.Add(fb);
					currentIndex = g * i + i;
					z++;
				}
				else
				{
					List<T> fb = source.GetRange(currentIndex, g);
					currentIndex = g * i + 1;
					list.Add(fb);
				}
			}
			return list;
		}
           

继续阅读