Ms Lyana

Ms Lyana

  • NA
  • 68
  • 2.6k

Display the minimum answer

May 4 2017 12:13 AM
Hi, I have problem to display my minimum value from the answer that I got.
Here is my output when I run :
  The problem is, at the minimum result it shows nothing. Supposely it will show like this
   9 7 6 =2 
because that is the minimum value (2).
Here is my code:
  1. class Program  
  2. {  
  3.     static void Main(string[] args)  
  4.     {  
  5.   
  6.         string input;  
  7.         int NoDisplay;  
  8.         decimal goal;  
  9.         decimal element;  
  10.   
  11.         do  
  12.         {  
  13.             Console.WriteLine("Please enter the target:");  
  14.             input = Console.ReadLine();  
  15.         }  
  16.         while (!decimal.TryParse(input, out goal));  
  17.   
  18.         Console.WriteLine("Please enter the numbers (separated by spaces)");  
  19.         input = Console.ReadLine();  
  20.         string[] elementsText = input.Split(' ');  
  21.         List elementsList = new List();  
  22.         foreach (string elementText in elementsText)  
  23.         {  
  24.             if (decimal.TryParse(elementText, out element))  
  25.             {  
  26.                 elementsList.Add(element);  
  27.   
  28.             }  
  29.   
  30.         }  
  31.   
  32.         int i;  
  33.         int j;  
  34.         decimal tmp;  
  35.   
  36.   
  37.         for (i = 0; i < elementsList.Count; i++)  
  38.   
  39.         {  
  40.             for (j = i + 1; j < elementsList.Count; j++)  
  41.             {  
  42.                 if (elementsList[i] < elementsList[j])  
  43.                 {  
  44.                     tmp = elementsList[i];  
  45.                     elementsList[i] = elementsList[j];  
  46.                     elementsList[j] = tmp;  
  47.                 }  
  48.             }  
  49.         }  
  50.   
  51.         Console.WriteLine("Please enter the maximum combination :");  
  52.         NoDisplay = Convert.ToInt32(Console.ReadLine());  
  53.   
  54.         Console.WriteLine("The results is :"); //the first result  
  55.   
  56.         Solver solver = new Solver();  
  57.         List> results = solver.Solve(goal, elementsList.ToArray());  
  58.   
  59.         //results.Reverse();  
  60.   
  61.         int counter = 0;  
  62.   
  63.         Boolean recordexist = false;  
  64.         foreach (List result in results)  
  65.         {  
  66.   
  67.             if (counter == 3) break;  
  68.             if (result.Count == NoDisplay)  
  69.             {  
  70.   
  71.                 decimal sum = 0;  
  72.                 recordexist = true;  
  73.                 foreach (decimal value in result)  
  74.                 {  
  75.                     Console.Write("{0}\t", value);  
  76.                     sum = sum + value;  
  77.                 }  
  78.                 if (recordexist == true)  
  79.                 {  
  80.   
  81.                     Console.WriteLine(" = " + sum);  
  82.                 }  
  83.   
  84.                 counter++;  
  85.   
  86.             }  
  87.         }  
  88.   
  89.         if (recordexist == false)  
  90.         {  
  91.             Console.WriteLine("No record exist");  
  92.         }  
  93.   
  94.         Console.WriteLine("The final results is :"); //the last result  
  95.   
  96.         Solver solvers = new Solver();  
  97.         List> resultss = solvers.Solve(goal, elementsList.ToArray());  
  98.   
  99.         //results.Reverse();  
  100.   
  101.         int counters = 0;  
  102.   
  103.         Boolean recordexists = false;  
  104.   
  105.   
  106.         decimal minSub = 0;     //to holding minimun total    
  107.         List minResult = new List(); //to holding minimum Result  
  108.         foreach (List result in results)  
  109.         {  
  110.   
  111.             if (counters == 3) break;  
  112.             if (result.Count == NoDisplay)  
  113.             {  
  114.                 decimal subtract = 0;  
  115.                 decimal sum = 0;  
  116.                 recordexists = true;  
  117.                 foreach (decimal value in result)  
  118.                 {  
  119.   
  120.                     Console.Write("{0}\t", value);  
  121.                     sum = sum + value;  
  122.                     subtract = sum - goal;  
  123.                 }  
  124.   
  125.   
  126.                 if (subtract < minSub)  
  127.                 {  
  128.                     minSub = subtract;  
  129.                     foreach (decimal value in result)  
  130.   
  131.                     {  
  132.                         minResult.Add(value);  
  133.                     }  
  134.                 }  
  135.   
  136.                 if (recordexists == true)  
  137.   
  138.                 {  
  139.   
  140.                     Console.WriteLine(" = " + subtract);  
  141.                 }  
  142.   
  143.                     counters++;  
  144.   
  145.                 }  
  146.             }  
  147.           
  148.   
  149.             if (recordexists == false)  
  150.   
  151.             {  
  152.                 Console.WriteLine("No record exist");  
  153.             }  
  154.   
  155.         Console.WriteLine("The minimum results is :");   //the minimum result line  
  156.         foreach (decimal value in minResult)  
  157.   
  158.         {  
  159.   
  160.             Console.Write("{0}", minSub);  
  161.         }  
  162.   
  163.         Console.ReadLine();  
  164.   
  165.         }  
  166.   
  167.     } 
 Thanks in advance

Answers (8)