Merge stones 1 & 1 --> 2
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void Main()
{
List<int> stones = new List<int>() {1,1,2,4,5,5};
var max = stones.Max();
for(int j = 1; j <= max +1 ; j++){
var count = 0;
for (int i = 0; i < stones.Count; i ++){
if(stones[i] == j){
count = count + 1;
}
}
while (count > 1){
merge(stones, j);
count = count - 2;
}
}
foreach(var i in stones){
Console.WriteLine(i);
}
Console.WriteLine(stones.Count());
}
public static bool merge(List<int> stones, int number){
var toReturn = false;
var count = 0;
for (int i = 0; i < stones.Count; i ++){
if(stones[i] == number){
count = count + 1;
}
}
if(count >= 2){
toReturn = true;
stones.Remove(number);
stones.Remove(number);
stones.Add(number + 1);
}
return toReturn;
}
}
Aucun commentaire:
Enregistrer un commentaire