Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- public class Program
- {
- static void Main()
- {
- var result = Combinations(new List<string> { "1", "2", "3", "4", "5" }, 5);
- foreach (var combination in result)
- {
- Console.WriteLine(combination);
- }
- }
- static IEnumerable<string> Combinations(List<string> characters, int length)
- {
- for (int i = 0; i < characters.Count; i++)
- {
- // ако листът има 1 елемент, алгоритъмът го връща
- if (length == 1)
- yield return characters[i];
- // генерира всички комбинации, без повторения
- else
- foreach (string next in Combinations(characters.GetRange(i + 1, characters.Count - (i + 1)), length - 1))
- {
- yield return characters[i] + next;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement