Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Each element of the
- array represents the time needed to pass through that step (the index). There are going to be two cars. One of
- them starts from the left side and the other one starts from the right side. The middle index of the array is the
- finish line. The number of elements in the array will always be odd. Calculate the total time for each racer to reach
- the finish, which is the middle of the array, and print the winner with his total time (the racer with less time). If
- you have a zero in the array, you have to reduce the time of the racer that reached it by 20% (from his current
- time).*/
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace Car_Race
- {
- class Program
- {
- static void Main(string[] args)
- {
- Console.WriteLine("Enter the line of indexes: ");
- int[] indexes = Console.ReadLine().Split(" ").Select(int.Parse).ToArray();
- int indexesLength = indexes.Length;
- //List<int> firstRacer = new List<int>();
- //List<int> secondRacer = new List<int>();
- double firstRacerTime = 0;
- double secondRacerTime = 0;
- for (int i = 0; i < indexesLength / 2; i++)
- {
- if(indexes[i] == 0)
- {
- firstRacerTime *= 0.8;
- }
- else
- {
- firstRacerTime += indexes[i];
- }
- }
- for (int s = indexesLength-1; s >= indexesLength / 2 + 1; s--)
- {
- if (indexes[s] == 0)
- {
- secondRacerTime *= 0.8;
- }
- else
- {
- secondRacerTime += indexes[s];
- }
- }
- if(firstRacerTime < secondRacerTime)
- {
- Console.WriteLine("The winner is left with total time of: {0:F2}", firstRacerTime);
- }
- else
- {
- Console.WriteLine("The winner is right with total time of: {0:F2}", secondRacerTime);
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment