using System;
using System.Linq;
class Program
{
static void Main()
{
Console.WriteLine(GetLastStanding(11, 4));
}
static int GetLastStanding(int n, int step)
{
var items = Enumerable.Range(0, n)
.Select(position => new Item { Position = position }).ToArray();
var i = 0;
var stepCounter = 0;
var nCounter = n;
while (nCounter > 1)
{
if (!items[i].Marked && ++stepCounter == step)
{
stepCounter = 0;
items[i].Marked = true;
--nCounter;
Console.WriteLine(string.Join(", ",
items.Select(item => item.Marked ?
"_" : (item.Position + 1).ToString())));
}
i = (i + 1) % n;
}
return items.Single(item => !item.Marked).Position;
}
struct Item
{
public int Position;
public bool Marked;
}
}
internal class Program
{
private static void Main(string[] args)
{
foreach (var combination in GenerateCombinations(3, 5, 1))
{
foreach (var i in combination)
{
Console.Write(i + ", ");
}
Console.WriteLine();
}
Console.WriteLine("----------------");
foreach (var combination in GenerateUniqueCombinations(3, 5, 1))
{
foreach (var i in combination)
{
Console.Write(i + ", ");
}
Console.WriteLine();
}
Console.ReadLine();
}
private static List<List<int>> GenerateUniqueCombinations(int stacksCount, int totalAmount, int step)
{
var generatedCombinations = GenerateCombinations(stacksCount, totalAmount, step);
var dicts = new List<Dictionary<int, int>>();
foreach (var combination in generatedCombinations)
{
var dict = new Dictionary<int, int>();
foreach (var i in combination)
{
if (dict.ContainsKey(i))
{
dict[i]++;
}
else
{
dict[i] = 1;
}
}
if (!dicts.Any(d => DictionaryEquals(d, dict)))
{
dicts.Add(dict);
}
}
return dicts.Select(d =>
{
var r = new List<int>();
foreach (var key in d.Keys)
{
if (d[key] == 1)
{
r.Add(key);
}
else
{
r.AddRange(Enumerable.Repeat(key, d[key]));
}
}
return r;
}).ToList();
}
private static bool DictionaryEquals(Dictionary<int, int> x, Dictionary<int, int> y)
{
foreach (var key in x.Keys)
{
if (!y.ContainsKey(key))
{
return false;
}
if (x[key] != y[key])
{
return false;
}
}
foreach (var key in y.Keys)
{
if (!x.ContainsKey(key))
{
return false;
}
if (x[key] != y[key])
{
return false;
}
}
return true;
}
private static List<List<int>> GenerateCombinations(int stacksCount, int totalAmount, int step)
{
var result = new List<List<int>>();
if (stacksCount > 1)
{
for (var i = 1; i < totalAmount / step; ++i)
{
var add = i * step;
foreach (var generated in GenerateCombinations(stacksCount - 1, totalAmount - add, step))
{
generated.Insert(0, add);
result.Add(generated);
}
}
}
else
{
result.Add(new List<int> { totalAmount });
}
return result;
}
}
public interface INotifyProgress
{
void Notify(int percent);
}
the C# compiler was not designed to be a component of a security system, so don't use it as one