Advertisement
wingman007

CoinsCombinations2023

Nov 7th, 2023
613
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.17 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. public class CoinCombinations
  5. {
  6.     private static readonly int[] Coins = { 1, 2, 5, 10, 20, 50 };
  7.     private static readonly List<List<int>> Combinations = new List<List<int>>();
  8.  
  9.     public static void Main()
  10.     {
  11.         FindCombinations(new List<int>(), 100);
  12.         foreach (var combination in Combinations)
  13.         {
  14.             Console.WriteLine(string.Join(" ", combination));
  15.         }
  16.     }
  17.  
  18.     public static void FindCombinations(List<int> currentCombination, int remaining)
  19.     {
  20.         if (remaining == 0)
  21.         {
  22.             Combinations.Add(new List<int>(currentCombination));
  23.             return;
  24.         }
  25.  
  26.         if (remaining < 0)
  27.         {
  28.             return;
  29.         }
  30.  
  31.         for (int i = 0; i < Coins.Length; i++)
  32.         {
  33.             if (currentCombination.Count == 0 || Coins[i] >= currentCombination[currentCombination.Count - 1])
  34.             {
  35.                 currentCombination.Add(Coins[i]);
  36.                 FindCombinations(currentCombination, remaining - Coins[i]);
  37.                 currentCombination.RemoveAt(currentCombination.Count - 1);
  38.             }
  39.         }
  40.     }
  41. }
  42.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement