Answer the question
In order to leave comments, you need to log in
How to improve the algorithm?
There is such a task:
"There are 54 seats in a reserved seat car, which are located in nine compartments. Seats from 1 to 36 are basic and they are located four in a compartment (1 - 4 in the first, ..., 33 - 36 in the ninth), from 37 up to 54 - side, divided into two, but the location of the compartment is reversed: places 37, 38 are in the ninth compartment, 39 and 40 in the eighth, ..., 53 and 54 in the first. Determine the compartment number by the seat number.
I solved it like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
private static int _placesCount;
private static int _roomCount = 18;
private static int _mainRoomPlacesCount = 4;
private static int _sideRoomPlacesCount = 2;
static void Main(string[] args)
{
_placesCount = (_roomCount / 2 * _mainRoomPlacesCount) + (_roomCount / 2 * _sideRoomPlacesCount);
while(true)
{
var number = 0;
Console.WriteLine("\n\nВведите номер места:");
var ParceResult = int.TryParse(Console.ReadLine(), out number);
if (ParceResult)
{
if(number <= _placesCount && number > 0)
{
var result = FindRoomByPlace(number);
Console.WriteLine("Вывод: " + result);
}
else
{
Console.WriteLine("Такого места не существует!");
}
}
else
{
Console.WriteLine("Ошибка. Не удалось преобразовать строку в число.");
}
}
}
private static double FindRoomByPlace(int placeNumber)
{
var mainPlacesCount = _roomCount / 2 * _mainRoomPlacesCount;
if(placeNumber <= mainPlacesCount)
{
return Math.Ceiling((double)placeNumber / _mainRoomPlacesCount);
}
else
{
return Math.Ceiling((double)(_placesCount - placeNumber + 0.5f) / _sideRoomPlacesCount);
}
}
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question