private void CalculateFitness(TimeTable timeTable)
{
int score = 0, DAYS_NUM = 5;
score = timeTable.Exams.SelectMany(exam => exam.Students)
.GroupBy(s => s)
.Select(g => Connections(g.Count()))
.Sum();
timeTable.Fitness = score;
}
int Connections(int corners)
{
// 0+1+2+...+(corners-1)
return corners * (corners - 1) / 2;
}
1 に答える
1
あなたの機能はこれと同等ではありませんか:
score = timeTable.Exams.SelectMany(exam=>exam.Students)
.GroupBy(s=>s)
.Select(g=>Connections(g.Count()))
.Sum();
ヘルパー機能付き
int Connections(int corners)
{
//Formula for number of sides in a complete graph
//http://en.wikipedia.org/wiki/Complete_graph
// 0+1+2+...+(corners-1)
return corners*(corners-1)/2;
}
これは線形ランタイムである必要がありますがtimeTable.Exams.Sum(exam=>exam.Student.Count())
、あなたのものは私には二次関数に見えます。
于 2011-01-12T15:00:42.787 に答える