using System;using System.Linq;publicclassPlayer{publicstring Name;publicint Score;publicPlayer(string Q,int S){ Name = Q; Score = S;}}publicclassFiveASide{publicstaticintChooseSides(Player[] Q){int U = Q.Sum(V => V.Score), R = U;for(var L =1<< Q.Length;0< L;){var C =0;for(var S =--L;0< S;S /=2)if(1== S %2)++C;if(C == Q.Length /2){
C =0;for(var F = Q.Length;0< F--;)if(1==(1& L >> F)) C += Q[F].Score;
R = Math.Min(R,Math.Abs(C + C - U));}}return R;}}
答案3:
using System;using System.Collections.Generic;using System.Linq;publicclassPlayer{publicstring Name {get;}publicint Score {get;}privatePlayer(){}publicPlayer(string name,int score){
Name = name;
Score = score;}}publicclassFiveASide{publicstaticintChooseSides(Player[] players){// Handle some edge casesif(players ==null|| players.Length ==0)return0;if(players.Length ==1)return players.Single().Score;// Intialize some variables to find the lowest disparity between 2 team's scoresvar n = players.Length;var k = n /2;var sum = players.Sum(p => p.Score);int minScore =int.MaxValue;// Make use of a stack to perform "n Choose k" combinations of playersvar combo =newPlayer[k];var stack =newStack<int>(k);
stack.Push(0);while(stack.Count >0){int comboIndex = stack.Count -1;int listIndex = stack.Pop();while(listIndex < n){
combo[comboIndex++]= players[listIndex++];
stack.Push(listIndex);if(comboIndex != k)continue;// We've reached a new combination of k elements - check if its the new lowest differencevar side = combo.Sum(p => p.Score);var diff = Math.Abs(sum - side - side);if(diff < minScore) minScore = diff;if(minScore ==0)return0;break;}}return minScore;}}
using System;using System.Collections.Generic;using System.Linq;using System.Collections;publicclassPlayer{publicstring Name {get;}publicint Score {get;}privatePlayer(){}publicPlayer(string name,int score){
Name = name;
Score = score;}}publicclassFiveASide{publicstaticintChooseSides(Player[] players){if(players.Count()==0){return0;}if(players.Count()==1){return players[0].Score;}returnExhaustive2(players);}privatestaticintExhaustive2(Player[] players){////Create teams // Find all comb of smaller team size from pool of players// min diff = 999999// For each combi// for each player// if in combi// add to team A// else // add to teamB// ] // ] // if score diff A B < minDiff update minDiff // ] //TeamsA and B give a valid Pair of teams so can find the diff//giving the Min Diff//int numPlayers = players.Length;int GrandTotalScore = players.Sum(s => s.Score);int[] fullSequence = Enumerable.Range(0, numPlayers).ToArray();bool even =(numPlayers %2==0);int TeamAsize = numPlayers /2;
List<List<int>> Combis = Combination.IntCombinations(fullSequence, TeamAsize);int minDiff =int.MaxValue;foreach(var combi in Combis){
List<Player> TeamA =newList<Player>();
List<Player> TeamB =newList<Player>();for(int i =0; i < players.Length; i++){Player p =(Player)players[i];if(combi.Contains(i)){
TeamA.Add(p);}else{
TeamB.Add(p);}}int diff = Math.Abs(TeamA.Sum(a => a.Score)- TeamB.Sum(b => b.Score));if(diff<minDiff){
minDiff = diff;}}return minDiff;}}publicstaticclassCombination{privatestaticboolNextCombination(IList<int> num,int n,int k){bool finished;var changed = finished =false;if(k <=0)returnfalse;for(var i = k -1;!finished &&!changed; i--){if(num[i]< n -1-(k -1)+ i){
num[i]++;if(i < k -1)for(var j = i +1; j < k; j++)
num[j]= num[j -1]+1;
changed =true;}
finished = i ==0;}return changed;}publicstatic IEnumerable Combinations<T>(IEnumerable<T> elements,int k){var elem = elements.ToArray();var size = elem.Length;if(k > size)yieldbreak;var numbers =newint[k];for(var i =0; i < k; i++)
numbers[i]= i;do{yieldreturn numbers.Select(n => elem[n]);}while(NextCombination(numbers, size, k));}publicstatic IEnumerable<IEnumerable<T>>Combinations2<T>(this IEnumerable<T> elements,int k){return k ==0?new[]{newT[0]}:
elements.SelectMany((e, i)=>
elements.Skip(i +1).Combinations2(k -1).Select(c =>(new[]{ e }).Concat(c)));}publicstatic List<List<int>>IntCombinations<T>(this IEnumerable<T> elements,int k){var a =Combinations2(elements, k);
List<List<int>> outer =newList<List<int>>();foreach(var o in a){
List<int> inner =newList<int>();foreach(var i in o){
inner.Add((int)(object)i);}
outer.Add(inner);}return outer;}}