Ms Lyana

Ms Lyana

  • NA
  • 68
  • 2.5k

c# how to sum all output from combination that i get

May 2 2017 2:03 AM
Please, I really need help. I have code that ask user to key-in the target, random number and how much combination that user want. Then, the output must bigger than the target .
 
This is the output :
So how can i change this output to be like this :
 
    9 8 7 = 24
    9 8 6 = 23
    9 7 6 = 22
 
And then after sum the number, I need to subtract the answer with the target that user key-in. Example like this: 
 
    24 - 20 = 4
    23 - 20 = 3
    22 - 20 = 2
 
Here is my code that I do to get the output like the image:
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7.   
  8. class Program  
  9. {  
  10. static void Main(string[] args)  
  11. {  
  12.   
  13. string input;  
  14. int NoDisplay;  
  15. decimal goal;  
  16. decimal element;  
  17.   
  18. do  
  19. {  
  20. Console.WriteLine("Please enter the target:");  
  21. input = Console.ReadLine();  
  22. }  
  23. while (!decimal.TryParse(input, out goal));  
  24.   
  25. Console.WriteLine("Please enter the numbers (separated by spaces)");  
  26. input = Console.ReadLine();  
  27. string[] elementsText = input.Split(' ');  
  28. List elementsList = new List();  
  29. foreach (string elementText in elementsText)  
  30. {  
  31. if (decimal.TryParse(elementText, out element))  
  32. {  
  33. elementsList.Add(element);  
  34.   
  35. }  
  36.   
  37. }  
  38.   
  39. int i;  
  40. int j;  
  41. decimal tmp;  
  42. int[] arr1 = new int[10];  
  43.   
  44. for (i = 0; i < elementsList.Count; i++)  
  45.   
  46. {  
  47. for (j = i + 1; j < elementsList.Count; j++)  
  48. {  
  49. if (elementsList[i] < elementsList[j])  
  50. {  
  51. tmp = elementsList[i];  
  52. elementsList[i] = elementsList[j];  
  53. elementsList[j] = tmp;  
  54. }  
  55. }  
  56. }  
  57.   
  58.   
  59.   
  60.   
  61. Console.WriteLine("Please enter the maximum combination :");  
  62. NoDisplay = Convert.ToInt32(Console.ReadLine());  
  63.   
  64.   
  65. Solver solver = new Solver();  
  66. List> results = solver.Solve(goal, elementsList.ToArray());  
  67.   
  68. //results.Reverse();  
  69.   
  70. int counter = 0;  
  71.   
  72. Boolean recordexist = false;  
  73. foreach (List result in results)  
  74. {  
  75.   
  76. if (counter == 3) break;  
  77. if (result.Count == NoDisplay)  
  78. {  
  79. recordexist = true;  
  80. foreach (decimal value in result)  
  81. {  
  82. Console.Write("{0}\t", value);  
  83. }  
  84. if (recordexist == true)  
  85. {  
  86.   
  87. Console.WriteLine();  
  88. }  
  89.   
  90. counter++;  
  91.   
  92. }  
  93. }  
  94.   
  95. if (recordexist == false)  
  96. {  
  97. Console.WriteLine("No record exist");  
  98. }  
  99.   
  100. Console.ReadLine();  
  101. }  
  102. }  
  103.   
  104. public class Solver  
  105. {  
  106.   
  107. private List> mResults;  
  108.   
  109. public List> Solve(decimal goal, decimal[] elements)  
  110. {  
  111.   
  112. mResults = new List>();  
  113.   
  114. RecursiveSolve(goal, 0.0m,  
  115. new List(), new List(elements), 0);  
  116. return mResults;  
  117. }  
  118.   
  119.   
  120. private void RecursiveSolve(decimal goal, decimal currentSum,  
  121. List included, List notIncluded, int startIndex)  
  122. {  
  123.   
  124. for (int index = startIndex; index < notIncluded.Count; index++)  
  125. {  
  126.   
  127. decimal nextValue = notIncluded[index];  
  128. if (currentSum + nextValue > goal) //bigger than target  
  129. {  
  130.   
  131. List newResult = new List(included);  
  132. newResult.Add(nextValue);  
  133. mResults.Add(newResult);  
  134.   
  135.   
  136.   
  137. }  
  138.   
  139. else if (currentSum + nextValue < goal)  
  140. {  
  141.   
  142. List nextIncluded = new List(included);  
  143. nextIncluded.Add(nextValue);  
  144. List nextNotIncluded = new List(notIncluded);  
  145. nextNotIncluded.Remove(nextValue);  
  146. RecursiveSolve(goal, currentSum + nextValue,  
  147. nextIncluded, nextNotIncluded, startIndex++);  
  148. }  
  149.   
  150.   
  151. }  
  152.   
  153.   
  154.   
  155. }  
  156.   
  157. }  

Answers (2)