Write a program that calculates what is the distance passed by a car (in kilometers), if we know the initial speed (km/h), the initial time frame in minutes, then the speed is increased by 10%, the second time frame, then the speed is decreased by 5%, and the time until the end of the trip. In order to calculate the distance, you need to convert the minutes into hours (for example 70 minutes = 1.1666 hours).
4 rows are read from the console:
- The initial speed in km/h – an integer within the range [1 … 300].
- The first time frame in minutes – an integer within the range [1 … 1000].
- The second time frame in minutes – an integer within the range [1 … 1000].
- The third time frame in minutes – an integer within the range [1 … 1000].
Print a number in the console: the kilometers passed, formatted up to the second symbol after the decimal point.
|330.90||Distance with initial speed: 90 km/h * 1 hr (60 min) = 90 km
After speed increase: 90 + 10% = 99.00 km/h * 1.166 hr (70 min) = 115.50 km
After speed decrease: 99 - 5% = 94.05 km/h * 1.33 hr (80 min) = 125.40 km
Total number of km passed: 330.9 km
|917.12||Distance with initial speed: 140 km/h * 1.86 hr (112 min) = 261.33 km
After speed increase: 140 + 10% = 154.00 km/h * 1.25 hr (75 min) = 192.5 km
After speed decrease: 154.00 - 5% = 146.29 km/h * 3.16 hr (190 min) = 463.28 km
Total number of km passed: 917.1166 km
It is possible that such a description may look misleading and incomplete at first glance, which adds to the complexity of a relatively easy task. Let's separate the problem into a few sub-problems and try to solve each of them one by one, which will lead us to the final result:
- Our initial sub-problem will be to read the input data entered by the user, and store them in appropriate variables.
- Execution of the main programming logic, which in our case is a batch of simple calculations of the properties that we already have.
- Calculation and shaping up the end result.
The main part of the programming logic is to calculate what will be the distance passed after all changes in speed. As during execution of the program, part of the data that we have is modified, we could separate the program code into a few logically separated parts:
- Calculation of the distance passed with initial speed.
- Change of speed and calculation of the distance passed.
- Last change of speed and calculation.
- Summing up.
We initially store one variable that will be used multiple times. This centralization approach gives us flexibility and possibility to modify the end result of the program with minimum efforts. In case we need to change the value, we must do it in only once place in the code, which saves us time and effort.
|Avoiding repetitive code (centralization of the program logic) in the tasks that we examine in the present book may look unnecessary at first glance, but this approach is very important upon building large applications in a real work environment, and its exercising in an initial stage of training will help you build a quality programming style.|
In order to perform the calculations we select
|The data type for real numbers with decimal representation in C# is the 128-bit
We calculate the travel time (in hours) by dividing the time by 60 (minutes in an hour). The travel distance is calculated by multiplying the starting speed by the time passed (in hours). After that we change the speed by increasing it by 10%, as per the task description. Calculating the percentage, as well as the following distances passed, is done in the following way:
- The time frame (in hours) is calculated by dividing the provided time frame in minutes by the minutes that are contained in an hour (60).
- The distance passed is calculated by multiplying the time frame (in hours) by the speed that is received after the increase.
- The next step is to decrease the speed by 5%, as per the problem description.
- We calculate the remaining distance in the manner described in the first two points.
Up until now we were able to solve two of the most important sub-problems, namely the data input and their processing. What remains is to calculate the end result. As by the description we are required to format it up to 2 symbols after the decimal point, we can do this in the following manner:
If you worked accurately and wrote the program using the input data given in the task description, you will be convinced that it works properly.
Test your solution here: https://judge.softuni.bg/Contests/Practice/Index/517#0.