9
Answers

How to display the smallest answer

Ms Lyana

Ms Lyana

7y
277
1
Hi currently I have some code that will display several answer. My problem is how can I display the smallest answer only.
Here is my output:
For the final result, I just want display the smallest answer. Like this:
 
    9 7 6 = 2
 
Only that answer I want to display. How can I do that?
 
Here is my code:
 
  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<decimal> elementsList = new List<decimal>();  
  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.         Console.WriteLine("Please enter the maximum combination :");  
  60.         NoDisplay = Convert.ToInt32(Console.ReadLine());  
  61.   
  62.         Console.WriteLine("The results is :");  
  63.   
  64.         Solver solver = new Solver();  
  65.         List<List<decimal>> results = solver.Solve(goal, elementsList.ToArray());  
  66.   
  67.         //results.Reverse();  
  68.   
  69.         int counter = 0;   
  70.   
  71.         Boolean recordexist = false;  
  72.         foreach (List<decimal> result in results)  
  73.         {  
  74.   
  75.             if (counter == 3) break;  
  76.             if (result.Count == NoDisplay)   
  77.             {  
  78.                 decimal subtract = 0;  
  79.                 decimal sum = 0;  
  80.                 recordexist = true;  
  81.                 foreach (decimal value in result)  
  82.                 {  
  83.                     Console.Write("{0}\t", value);  
  84.                     sum = sum + value;  
  85.                                    }  
  86.                 if (recordexist == true)  
  87.                 {  
  88.                       
  89.                     Console.WriteLine(" = " + sum);  
  90.                 }  
  91.   
  92.                 counter++;  
  93.   
  94.             }  
  95.         }  
  96.   
  97.         if (recordexist == false)  
  98.         {  
  99.             Console.WriteLine("No record exist");  
  100.         }  
  101.   
  102.         Console.WriteLine("The final results is :");  
  103.   
  104.         Solver solvers = new Solver();  
  105.         List<List<decimal>> resultss = solvers.Solve(goal, elementsList.ToArray());  
  106.   
  107.         //results.Reverse();  
  108.   
  109.         int counters = 0;  
  110.   
  111.         Boolean recordexists = false;  
  112.         foreach (List<decimal> result in results)  
  113.         {  
  114.   
  115.             if (counters == 3) break;  
  116.             if (result.Count == NoDisplay)  
  117.             {  
  118.                 decimal subtract = 0;  
  119.                 decimal sum = 0;  
  120.                 recordexists = true;  
  121.                 foreach (decimal value in result)  
  122.                 {  
  123.                      
  124.                     Console.Write("{0}\t", value);  
  125.                     sum = sum + value;  
  126.                     subtract = sum - goal;  
  127.                       
  128.                 }  
  129.                 if (recordexists == true)  
  130.                 {  
  131.                     //decimal max = result.Max();  
  132.                     //Console.WriteLine(max);  
  133.   
  134.                     Console.WriteLine(" = " + subtract);  
  135.                 }  
  136.   
  137.                 counters++;  
  138.   
  139.             }  
  140.         }  
  141.   
  142.         if (recordexists == false)  
  143.         {  
  144.             Console.WriteLine("No record exist");  
  145.         }  
  146.   
  147.         Console.ReadLine();  
  148.     }  

 
Answers (9)