Algorithm 合并两个列表并均匀分布内容的算法
比如说,Algorithm 合并两个列表并均匀分布内容的算法,algorithm,sorting,Algorithm,Sorting,比如说, A { H, H, H, H, H, H, H, H } B { T, T, T, T, T, T, T, T, T, T } A+B { T, H, T, H, T, T, H, T, H, T, H, T, T, H, T, H, T, H } 我找不到解决这个问题的好办法,但我觉得必须有一个明确的答案。 有人有什么想法吗?如果将来有人发现这一点,这是我做的算法。它并不完美,但似乎很适合我的需要 static List<T> CombineLists<T>
A { H, H, H, H, H, H, H, H }
B { T, T, T, T, T, T, T, T, T, T }
A+B { T, H, T, H, T, T, H, T, H, T, H, T, T, H, T, H, T, H }
我找不到解决这个问题的好办法,但我觉得必须有一个明确的答案。
有人有什么想法吗?如果将来有人发现这一点,这是我做的算法。它并不完美,但似乎很适合我的需要
static List<T> CombineLists<T>( List<T> listA, List<T> listB )
{
List<T> combinedList = new List<T>();
List<T> longerList = (listA.Count > listB.Count) ? listA : listB;
List<T> shorterList = (listA.Count > listB.Count) ? listB : listA;
float listLengthRatio = (float)longerList.Count / (float)shorterList.Count;
int longListCount = 0;
int shortListCount = 0;
while (longListCount < longerList.Count ||
shortListCount < shorterList.Count)
{
while ( longListCount < shortListCount * listLengthRatio )
{
if ( longListCount < longerList.Count )
{
combinedList.Add( longerList[longListCount] );
longListCount++;
}
}
if ( shortListCount < shorterList.Count )
{
combinedList.Add( shorterList[shortListCount] );
shortListCount++;
}
}
return combinedList;
}
静态列表组合列表(列表A、列表B)
{
List combinedList=新列表();
List longerList=(listA.Count>listB.Count)?listA:listB;
ListShorterList=(listA.Count>listB.Count)?listB:listA;
float list lengthratio=(float)longerList.Count/(float)shorterList.Count;
int longListCount=0;
int shortListCount=0;
而(longListCount
假设B.length>=A.length
(如果不只是交换引用)。将B.lenght/A.length
元素放入B列表,然后从列表中选择一个元素并重复,请记住,如果出现不均匀分割,您有时需要再放入一个元素。是的,正是这种不均匀分割的情况一直困扰着我。在上述情况下,10/8=1.25。我怎样才能轻松地考虑到这一点,并知道何时添加双打。