TheBulgarianWolf

Car Race

Dec 27th, 2020
993
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.14 KB | None | 0 0
  1. /*Each element of the
  2. array represents the time needed to pass through that step (the index). There are going to be two cars. One of
  3. them starts from the left side and the other one starts from the right side. The middle index of the array is the
  4. finish line. The number of elements in the array will always be odd. Calculate the total time for each racer to reach
  5. the finish, which is the middle of the array, and print the winner with his total time (the racer with less time). If
  6. you have a zero in the array, you have to reduce the time of the racer that reached it by 20% (from his current
  7. time).*/
  8.  
  9.  
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Linq;
  13.  
  14. namespace Car_Race
  15. {
  16.     class Program
  17.     {
  18.         static void Main(string[] args)
  19.         {
  20.             Console.WriteLine("Enter the line of indexes: ");
  21.             int[] indexes = Console.ReadLine().Split(" ").Select(int.Parse).ToArray();
  22.             int indexesLength = indexes.Length;
  23.             //List<int> firstRacer = new List<int>();
  24.             //List<int> secondRacer = new List<int>();
  25.             double firstRacerTime = 0;
  26.             double secondRacerTime = 0;
  27.             for (int i = 0; i < indexesLength / 2; i++)
  28.             {
  29.                 if(indexes[i] == 0)
  30.                 {
  31.                     firstRacerTime *= 0.8;
  32.                 }
  33.                 else
  34.                 {
  35.                     firstRacerTime += indexes[i];
  36.                 }
  37.             }
  38.  
  39.             for (int s = indexesLength-1; s >= indexesLength / 2 + 1; s--)
  40.             {
  41.                 if (indexes[s] == 0)
  42.                 {
  43.                     secondRacerTime *= 0.8;
  44.                 }
  45.                 else
  46.                 {
  47.                     secondRacerTime += indexes[s];
  48.                 }
  49.             }
  50.  
  51.             if(firstRacerTime < secondRacerTime)
  52.             {
  53.                 Console.WriteLine("The winner is left with total time of: {0:F2}", firstRacerTime);
  54.             }
  55.             else
  56.             {
  57.                 Console.WriteLine("The winner is right with total time of: {0:F2}", secondRacerTime);
  58.             }
  59.         }
  60.     }
  61. }
  62.  
Add Comment
Please, Sign In to add comment