Хабр Курсы для всех
РЕКЛАМА
Практикум, Хекслет, SkyPro, авторские курсы — собрали всех и попросили скидки. Осталось выбрать!
List<Rule> rules = ...
Collections.sort(rules, sortByScoreDescending);
List<Rule> rules = ...
for(List<Rule> orderedRules : perestanovka(rules)) {
// Посчитать для каждого варианта применения правил и выбрать вариант с самым большим score.
}
1 X X X X — 100 очков
5 X X X X — 50 очков
1 1 1 X X — 1000 очков
2 2 2 X X — 200 очков
3 3 3 X X — 300 очков
4 4 4 X X — 400 очков
5 5 5 X X — 500 очков
6 6 6 X X — 600 очков
int GetScore(int[] roles)
{
List<int> rolesList = roles.ToList();
Func<int> getScoreFromBestTemplate = () =>
{
var templatesWithScores = new []
{
new { Template = new List<int> {1}, Score = 100 },
new { Template = new List<int> {5}, Score = 50 },
new { Template = new List<int> {1, 1, 1}, Score = 1000 },
new { Template = new List<int> {2, 2, 2}, Score = 200 },
new { Template = new List<int> {3, 3, 3}, Score = 300 },
new { Template = new List<int> {4, 4, 4}, Score = 400 },
new { Template = new List<int> {5, 5, 5}, Score = 500 },
new { Template = new List<int> {6, 6, 6}, Score = 500 },
};
Func<List<int>, List<int>, List<int>> deleteMatchElements = (source, template) =>
{
var sourceCopy = source.ToList();
template.ForEach(item => sourceCopy.Remove(item));
return sourceCopy;
};
Func<List<int>, List<int>, bool> isMatch =
(source, template) => deleteMatchElements(source, template).Count == source.Count - template.Count;
var templateWithMaxScore =
(from tws in templatesWithScores
where isMatch(rolesList, tws.Template)
orderby tws.Score descending
select tws
).FirstOrDefault() ?? new { Template = new List<int> {}, Score = 0 };
rolesList = deleteMatchElements(rolesList, templateWithMaxScore.Template);
return templateWithMaxScore.Score;
};
int sumScore = 0;
int score = 0;
do
{
score = getScoreFromBestTemplate();
sumScore += score;
}
while (score != 0);
return sumScore;
}
new { Template = new List<int> {1, 1, 2, 2, 3 }, Score = 350 }
1 X X X X — 100 очков
5 X X X X — 50 очков
1 1 1 X X — 1000 очков
2 2 2 X X — 200 очков
3 3 3 X X — 300 очков
4 4 4 X X — 400 очков
5 5 5 X X — 500 очков
6 6 6 X X — 600 очков
Множественные ветвления и шаблон «Правила»