Advertisement
wingman007

MIITITMOM2023Solutions

Oct 30th, 2023
652
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.65 KB | None | 0 0
  1. // ----------------------------------------------- all combinations 0 ------------------------------
  2. // I have 5 integer numbers with positive and negative values. Please write a program on C# to find all combinations between the 5 numbers which are 0?
  3. using System;
  4. using System.Collections.Generic;
  5.  
  6. public class ZeroSumCombinations
  7. {
  8.     public static void Main()
  9.     {
  10.         int[] numbers = { 2, -2, 3, -3, 4 }; // Example input
  11.  
  12.         // Pairs
  13.         for (int i = 0; i < 5; i++)
  14.         {
  15.             for (int j = i + 1; j < 5; j++)
  16.             {
  17.                 if (numbers[i] + numbers[j] == 0)
  18.                 {
  19.                     Console.WriteLine($"Pair: {numbers[i]}, {numbers[j]}");
  20.                 }
  21.             }
  22.         }
  23.  
  24.         // Triples
  25.         for (int i = 0; i < 5; i++)
  26.         {
  27.             for (int j = i + 1; j < 5; j++)
  28.             {
  29.                 for (int k = j + 1; k < 5; k++)
  30.                 {
  31.                     if (numbers[i] + numbers[j] + numbers[k] == 0)
  32.                     {
  33.                         Console.WriteLine($"Triple: {numbers[i]}, {numbers[j]}, {numbers[k]}");
  34.                     }
  35.                 }
  36.             }
  37.         }
  38.  
  39.         // Quadruples
  40.         for (int i = 0; i < 5; i++)
  41.         {
  42.             for (int j = i + 1; j < 5; j++)
  43.             {
  44.                 for (int k = j + 1; k < 5; k++)
  45.                 {
  46.                     for (int l = k + 1; l < 5; l++)
  47.                     {
  48.                         if (numbers[i] + numbers[j] + numbers[k] + numbers[l] == 0)
  49.                         {
  50.                             Console.WriteLine($"Quadruple: {numbers[i]}, {numbers[j]}, {numbers[k]}, {numbers[l]}");
  51.                         }
  52.                     }
  53.                 }
  54.             }
  55.         }
  56.  
  57.         // Quintuples
  58.         if (numbers[0] + numbers[1] + numbers[2] + numbers[3] + numbers[4] == 0)
  59.         {
  60.             Console.WriteLine($"Quintuple: {numbers[0]}, {numbers[1]}, {numbers[2]}, {numbers[3]}, {numbers[4]}");
  61.         }
  62.     }
  63. }
  64.  
  65. // ----------------------------- Above without arrays ------------------------
  66. using System;
  67.  
  68. public class ZeroSumCombinations
  69. {
  70.     public static void Main()
  71.     {
  72.         int a = 2, b = -2, c = 3, d = -3, e = 4; // Example input
  73.  
  74.         // Pairs
  75.         if (a + b == 0) Console.WriteLine($"Pair: {a}, {b}");
  76.         if (a + c == 0) Console.WriteLine($"Pair: {a}, {c}");
  77.         if (a + d == 0) Console.WriteLine($"Pair: {a}, {d}");
  78.         if (a + e == 0) Console.WriteLine($"Pair: {a}, {e}");
  79.         if (b + c == 0) Console.WriteLine($"Pair: {b}, {c}");
  80.         if (b + d == 0) Console.WriteLine($"Pair: {b}, {d}");
  81.         if (b + e == 0) Console.WriteLine($"Pair: {b}, {e}");
  82.         if (c + d == 0) Console.WriteLine($"Pair: {c}, {d}");
  83.         if (c + e == 0) Console.WriteLine($"Pair: {c}, {e}");
  84.         if (d + e == 0) Console.WriteLine($"Pair: {d}, {e}");
  85.  
  86.         // Triples
  87.         if (a + b + c == 0) Console.WriteLine($"Triple: {a}, {b}, {c}");
  88.         if (a + b + d == 0) Console.WriteLine($"Triple: {a}, {b}, {d}");
  89.         if (a + b + e == 0) Console.WriteLine($"Triple: {a}, {b}, {e}");
  90.         if (a + c + d == 0) Console.WriteLine($"Triple: {a}, {c}, {d}");
  91.         if (a + c + e == 0) Console.WriteLine($"Triple: {a}, {c}, {e}");
  92.         if (a + d + e == 0) Console.WriteLine($"Triple: {a}, {d}, {e}");
  93.         if (b + c + d == 0) Console.WriteLine($"Triple: {b}, {c}, {d}");
  94.         if (b + c + e == 0) Console.WriteLine($"Triple: {b}, {c}, {e}");
  95.         if (b + d + e == 0) Console.WriteLine($"Triple: {b}, {d}, {e}");
  96.         if (c + d + e == 0) Console.WriteLine($"Triple: {c}, {d}, {e}");
  97.  
  98.         // Quadruples
  99.         if (a + b + c + d == 0) Console.WriteLine($"Quadruple: {a}, {b}, {c}, {d}");
  100.         if (a + b + c + e == 0) Console.WriteLine($"Quadruple: {a}, {b}, {c}, {e}");
  101.         if (a + b + d + e == 0) Console.WriteLine($"Quadruple: {a}, {b}, {d}, {e}");
  102.         if (a + c + d + e == 0) Console.WriteLine($"Quadruple: {a}, {c}, {d}, {e}");
  103.         if (b + c + d + e == 0) Console.WriteLine($"Quadruple: {b}, {c}, {d}, {e}");
  104.  
  105.         // Quintuples
  106.         if (a + b + c + d + e == 0) Console.WriteLine($"Quintuple: {a}, {b}, {c}, {d}, {e}");
  107.     }
  108. }
  109.  
  110.  
  111.  
  112. // You have 6 types of coins with values 1, 2, 5, 10, 20 and 50 cents. Write an algorithm on C# to find all combinations giving 100 cents (one dollar)
  113. // -------------------------- Coins -------------------------------------------------
  114. using System;
  115. using System.Collections.Generic;
  116.  
  117. public class CoinCombinations
  118. {
  119.     private static readonly int[] Coins =
  120.         { 1, 2, 5, 10, 20, 50 };
  121.     private static readonly List<List<int>> Combinations =
  122.         new List<List<int>>();
  123.  
  124.     public static void Main()
  125.     {
  126.         FindCombinations(new List<int>(), 100);
  127.         foreach (var combination in Combinations)
  128.         {
  129.             Console.WriteLine(string.Join(" ", combination));
  130.         }
  131.     }
  132.  
  133.     public static void FindCombinations(List<int> currentCombination, int remaining)
  134.     {
  135.         if (remaining == 0)
  136.         {
  137.             Combinations.Add(new List<int>(currentCombination));
  138.             return;
  139.         }
  140.  
  141.         if (remaining < 0)
  142.         {
  143.             return;
  144.         }
  145.  
  146.         for (int i = 0; i < Coins.Length; i++)
  147.         {
  148.             if (currentCombination.Count == 0 || Coins[i] >= currentCombination[currentCombination.Count - 1])
  149.             {
  150.                 currentCombination.Add(Coins[i]);
  151.                 FindCombinations(currentCombination, remaining - Coins[i]);
  152.                 currentCombination.RemoveAt(currentCombination.Count - 1);
  153.             }
  154.         }
  155.     }
  156. }
  157.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement