C# Fundamentals - Lists - more exercises. 2. Car Race
Здравейте, хора!
Днес се опитах да реша ето тази задачка:
2.Car Race
Write a program to calculate the winner of a car race. You will receive an array of numbers. 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).
Print the result in the following format "The winner is {left/right} with total time: {total time}".
Example
Input |
Output |
29 13 9 0 13 0 21 0 14 82 12 |
The winner is left with total time: 53.8 |
Comment |
|
The time of the left racer is (29 + 13 + 9) * 0.8 (because of the zero) + 13 = 53.8 The time of the right racer is (82 + 12 + 14) * 0.8 + 21 = 107.4 The winner is the left racer, so we print it |
А ето го и моя код:https://pastebin.com/qbHAWtGh
Линк към judge: https://judge.softuni.bg/Contests/Practice/Index/1300#1
Но нещо ми гърми judge и ми дава само 30/100, при положение че всичките мой входове ги изкарва добре кода. Моля за помощ да намеря грешката си, защото не мога да я забележа сам и моля за съвет как да не я допускам повече.
Благодаря!
Ето твоето решение вече дава 100/100...преправих него защото е по кратко от това на колегата Valko98
using System;
using System.Collections.Generic;
using System.Linq;
namespace _02._Car_Race
{
class Program
{
static void Main(string[] args)
{
List<int> time = Console.ReadLine()
.Split()
.Select(int.Parse)
.ToList();
double sumLeft = 0; // decimal е добре когато става въпрос за пари...тук може и дабъл да ползваме...даже judge дава 90/100 с децимал...
double sumRight = 0;
for (int i = 0; i < time.Count / 2; i++)
{
sumLeft += time[i];
if (time[i] == 0)
{
sumLeft *= 0.8;
}
//else
//{
// sumLeft += time[i];
//}
//if (time[time.Count - i - 1] == 0)
//{
// sumRight *= 0.8m;
//}
//else
//{
// sumRight += time[time.Count - i - 1];
//}
}
for (int i = time.Count - 1; i > time.Count / 2; i--)
{
sumRight += time[i];
if (time[i] == 0)
{
sumRight *= 0.8;
}
}
//if (sumLeft <= sumRight) // тук как да разберем кой е победителя ако времето им е И равно "<="
if (sumLeft < sumRight)
{
Console.WriteLine($"The winner is left with total time: {sumLeft}");
}
else
{
Console.WriteLine($"The winner is right with total time: {sumRight}");
}
}
}
}
Благодаря, колега :)