统计问题-比较列表本身
本文关键字:列表 比较 问题 统计 | 更新日期: 2023-09-27 18:04:51
这是给你的CompSci或统计人员。你能告诉我,如果列表包含72,786个"东西",那么在循环结束时compacount的值是多少?我想是72,786^2-1,但我的老大脑已经很久没有像这样工作了。非常感谢您的时间和帮助!
List<thing> theList = new List<thing>();//list contains 73,786 "things"
private void compare()
{
int compareCount = 0;
for(int i = 0; i < theList.Count-1; i++)
{
for(int comp = i + 1; comp < theList.Count; comp++)
{
compare(theList[i], theList[comp]);
compareCount++;
}
}
}
代码中的compacount值为(72786^2 - 72786) / 2 = 2648864505
。我已经运行过了。正如现在所写的,没有必要在内循环中调用compare(theList[i], theList[comp])
(因为它不会以任何方式影响计数)。
这是我如何记住(n^2 - n)/2
公式的:n
选手的循环赛,每个选手正好遇到所有其他选手一次。
匹配计划是一个包含n
行和列(n * n = n^2
组合)的正方形。由于玩家不与自己对弈,因此必须减去从左上角到右下角对角线上的n
匹配(现在剩下n^2 - n
匹配)。对角线上方三角形中A
对B
的配对与下方三角形中B
对A
的配对相同(此类配对中存在(n^2 - n)/2
)。从n^2 - n
中减去这个数字得到(n^2 - n)/2
可能匹配的最终结果。