Facebook
From Emerald Bison, 6 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 232
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace KnapsackProblem
  8. {
  9.         class Program
  10.         {
  11.                 static List<int> values = new List<int>();
  12.                 static List<int> chosenValues = new List<int>();
  13.                
  14.                 static void Introduction()
  15.                 {
  16.                         Console.WriteLine("Press any key to continue");
  17.                         Console.ReadKey();
  18.                 }
  19.                 static int AddValue()
  20.                 {
  21.                         Console.WriteLine("Start entering the values");
  22.                         string _enteredText = "";
  23.                         int _numberOfItems = 0;
  24.                         while (_enteredText != "/stop")
  25.                         {
  26.                                 _enteredText = Console.ReadLine();
  27.                                 Console.WriteLine("To stop, type '/stop'");
  28.  
  29.                                 if (int.TryParse(_enteredText, out int value) && value > 0)
  30.                                 {
  31.                                         values.Add(value);
  32.                                         _numberOfItems++;
  33.                                        
  34.                                 }
  35.                                 else
  36.                                         Console.WriteLine("Number invalid. Enter another one");
  37.                         }
  38.  
  39.                         Console.Clear();
  40.                         Console.WriteLine("You have entered {0} items", _numberOfItems);
  41.                         Console.ReadKey();
  42.                         Console.Clear();
  43.  
  44.                         return _numberOfItems;
  45.                 }
  46.                 static int EnterKnapsackCapacity()
  47.                 {
  48.                         Console.WriteLine("Enter your knapsack capacity");
  49.  
  50.                         string _enteredText;
  51.                         bool _properCapacity = false;
  52.                         int _capacity = 0;
  53.                        
  54.  
  55.  
  56.                         while (_properCapacity == false)
  57.                         {
  58.                                 _enteredText = Console.ReadLine();
  59.                                 if (int.TryParse(_enteredText, out int _knapsackCapacity) && _knapsackCapacity > 0)
  60.                                 {
  61.                                         _properCapacity = true;
  62.                                         _capacity = _knapsackCapacity;
  63.                                 }
  64.                                 else
  65.                                         Console.WriteLine("Invalid knapsack capacity. Enter another one");
  66.                         }
  67.  
  68.                         Console.Clear();
  69.                         Console.WriteLine("Your knapsack capacity is {0}",_capacity );
  70.                         Console.ReadKey();
  71.                         Console.Clear();
  72.                         return _capacity;
  73.  
  74.                 }
  75.                 static void Main(string[] args)
  76.                 {
  77.                         Introduction();
  78.  
  79.                         int capacity = EnterKnapsackCapacity();
  80.                         int numberOfItems = AddValue();
  81.                         int[] valuesArray = new int[numberOfItems];
  82.                         bool[] ifPicked = new bool[numberOfItems];
  83.                         int totalWeight = 0;
  84.                         chosenValues = values;
  85.  
  86.                        
  87.                         Console.ReadKey();
  88.  
  89.                 }
  90.                
  91.  
  92.         }
  93.        
  94. }
  95.