krasi1105

03. Endurance Rally

Jan 24th, 2017
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.87 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace _03.Endurance_Rally
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             string[] driverNames = Console.ReadLine().Split();
  14.             Driver[] drivers = ReturnDrivers(driverNames);
  15.             double[] trackLayout = Console.ReadLine()
  16.                                   .Split()
  17.                                   .Select(double.Parse)
  18.                                   .ToArray();
  19.             int lastZone = trackLayout.Length - 1;
  20.             int[] checkpoints = Console.ReadLine()
  21.                                   .Split()
  22.                                   .Select(int.Parse)
  23.                                   .ToArray();
  24.             for (int i = 0; i < trackLayout.Length; i++)
  25.             {
  26.                 for (int j = 0; j < drivers.Length; j++)
  27.                 {
  28.                     bool isCheckpoint = checkpoints.Contains(i);
  29.                     if (isCheckpoint && drivers[j].fuel > 0)
  30.                     {
  31.                         drivers[j].fuel += trackLayout[i];
  32.                         drivers[j].zoneReached = i;
  33.                     }
  34.                     else
  35.                     {
  36.                         if (drivers[j].fuel > 0)
  37.                         {
  38.                             drivers[j].fuel -= trackLayout[i];
  39.                             drivers[j].zoneReached = i;
  40.                         }
  41.                     }
  42.                 }
  43.             }
  44.  
  45.             PrintOutput(drivers, lastZone);
  46.         }
  47.  
  48.         static Driver[] ReturnDrivers(string[] driverNames)
  49.         {
  50.             int driversCount = driverNames.Length;
  51.             Driver[] drivers = new Driver[driversCount];
  52.             for (int i = 0; i < driversCount; i++)
  53.             {
  54.                 string currentName = driverNames[i];
  55.                 byte currentFuel = (byte)currentName[0];
  56.  
  57.                 drivers[i] = new Driver()
  58.                 {
  59.                     name = currentName,
  60.                     fuel = currentFuel,
  61.                     zoneReached = 0
  62.                 };
  63.             }
  64.  
  65.             return drivers;
  66.         }
  67.  
  68.         static void PrintOutput(Driver[] drivers, int trackLength)
  69.         {
  70.             foreach (Driver item in drivers)
  71.             {
  72.                 if (item.zoneReached == trackLength && item.fuel > 0)
  73.                 {
  74.                     Console.WriteLine($"{item.name} - fuel left {item.fuel:f2}");
  75.                 }
  76.                 else
  77.                 {
  78.                     Console.WriteLine($"{item.name} - reached {item.zoneReached}");
  79.                 }
  80.             }
  81.         }
  82.     }
  83.  
  84.  
  85.     class Driver
  86.     {
  87.         public string name { get; set; }
  88.         public double fuel { get; set; }
  89.         public int zoneReached { get; set; }
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment