Facebook
From Wet Crow, 6 Years ago, written in C#.
Embed
Download Paste or View Raw
Hits: 214
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7.  
  8. namespace algorithmLPT
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.  
  15.  
  16.             int numberLoop = 0,minValueProcessors=0,indexMinValueProcessors=0,maxValueTasks;
  17.             List<string> lines = loadFile();
  18.             int processors = loadProcessors(lines);
  19.             int[] sumProcessors = Enumerable.Repeat(0, processors).ToArray();
  20.             List<int> tasks = loadTasks(lines);
  21.             List<List<int>> tasksInProcessors = new List<List<int>>();
  22.  
  23.             tasks.Sort((a, b) => -1 * a.CompareTo(b));
  24.  
  25.             foreach (int task in tasks)
  26.             {
  27.                 if (numberLoop < processors )
  28.                 {
  29.                     if (numberLoop == 0)
  30.                         minValueProcessors = task;
  31.                     List<int> test = new List<int>();
  32.                     test.Add(task);
  33.                     tasksInProcessors.Add(test);
  34.                     sumProcessors[numberLoop] +=task;
  35.                     if(sumProcessors[numberLoop] < minValueProcessors)
  36.                     {
  37.                         minValueProcessors = sumProcessors[numberLoop];
  38.                         indexMinValueProcessors = numberLoop;
  39.                     }
  40.                     numberLoop++;
  41.                 }
  42.                 else
  43.                 {
  44.                     tasksInProcessors[indexMinValueProcessors].Add(task);
  45.                     sumProcessors[indexMinValueProcessors] += task;
  46.                     minValueProcessors = sumProcessors[0];
  47.                     indexMinValueProcessors = 0;
  48.                     for(int i = 1 ; i < processors ; i++)
  49.                     {
  50.                         if (sumProcessors[i] < minValueProcessors)
  51.                         {
  52.                             minValueProcessors = sumProcessors[i];
  53.                             indexMinValueProcessors = i;
  54.                         }
  55.  
  56.                     }
  57.  
  58.                 }
  59.             }
  60.            foreach (List<int> a in tasksInProcessors)
  61.             {
  62.                 foreach (int b in a)
  63.                 {
  64.                     Console.Write("{0} ", b);
  65.                 }
  66.                 Console.WriteLine();
  67.             }
  68.             Console.WriteLine();
  69.             maxValueTasks = sumProcessors[0];
  70.             for (int i = 1; i < processors; i++)
  71.             {
  72.  
  73.                 if (maxValueTasks < sumProcessors[i])
  74.                     maxValueTasks = sumProcessors[i];
  75.             }
  76.             Console.WriteLine(maxValueTasks);
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.             System.Console.ReadLine();
  87.         }
  88.  
  89.         private static List<string> loadFile()
  90.         {
  91.             List<string> lines = new List<string>();
  92.             Console.Write("Name file: ");
  93.             string nameFile = Console.ReadLine();
  94.             try
  95.             {
  96.                 lines = File.ReadAllLines(AppDomain.CurrentDomain.BaseDirectory + "\" + nameFile + ".txt").ToList();
  97.                return lines;
  98.            }
  99.            catch (FileNotFoundException e)
  100.            {
  101.                Console.WriteLine("File not found ");
  102.                Console.WriteLine("Press Enter to continue");
  103.                Console.ReadLine();
  104.                Environment.Exit(1);
  105.                
  106.            }
  107.            return null;
  108.        }
  109.  
  110.        private static List<int> loadTasks(List<string> lines)
  111.        {
  112.            List<int> tasks = new List<int>();
  113.            try
  114.            {
  115.                for (int i = 0; i < 2; i++)
  116.                    lines.RemoveAt(0);
  117.                tasks = lines.ConvertAll(s => Int32.Parse(s));
  118.                //sumProcessors = Enumerable.Repeat(0, processors).ToArray();
  119.                return tasks;
  120.            }
  121.            catch (FormatException e)
  122.            {
  123.                Console.WriteLine("Bad Formatn Press Enter to continue");
  124.                Console.ReadLine();
  125.                Environment.Exit(1);
  126.            }
  127.            return null;
  128.  
  129.        }
  130.  
  131.        private static int loadProcessors(List<string> lines)
  132.        {
  133.            List<int> tasks = new List<int>();
  134.            try
  135.            {
  136.                int processors = Convert.ToInt32(lines[0]);
  137.                return processors;
  138.  
  139.            }
  140.            catch (FormatException e)
  141.            {
  142.                Console.WriteLine("Bad Formatn Press Enter to continue");
  143.                Console.ReadLine();
  144.                Environment.Exit(1);
  145.            }
  146.            return 0;
  147.  
  148.        }
  149.    }
  150. }
  151.