 + Нов въпрос danail2003 27 Точки

## Distance Calculator

Ако може помощ 40/100.

# Distance Calculator

Create a program that calculates what percentage you’ve travelled. First, you will receive how many steps you’ve made. Then, you will receive how long 1 step is in centimeters. Last, you will receive the distance you need to travel in meters.

Then you have to calculate what distance you have travelled with the steps given. Have in mind that every fifth step is 30% shorter than usual. You have to calculate what percentage of the distance you’ve travelled.

In the end, print the percentage of the distance travelled, formatted to the 2nd decimal place, in the following format:

"You travelled {percentage}% of the distance!"

## Input

• On the 1st line you will receive the steps made – an integer number in the range [0…100000]
• On the 2nd line you will receive the length of 1 step – a real number in the range [0.0…50.0]
• On the 3rd line you will receive the distance you need to travel – an integer number in the range [0…100000]

## Output

• In the end print the percentage of the distance travelled formatted to the 2nd decimal place in the format described above.

## Constraints

• The input will always be in the right format.
• Percentage can be over 100%.

## Examples

 Input Output 100 2 1 You travelled 188.00% of the distance! Comments The length of a step is 2 centimeters. Every fifth step will be 1.4 centimeters long. 20 shorter steps are made. The distance that has to be travelled is 1 meter. The distance travelled is 1.88 meters which is 188% of the distance that had to be travelled. 5000 7.5 500 You travelled 70.50% of the distance!

using System;

namespace Distance_Calculator
{
class Program
{
static void Main(string[] args)
{
decimal result = 0;
decimal shortStep = 0;

for (int i = 0; i < stepsMade; i++)
{
if (i % 5 == 0)
{
shortStep = lengthOfStep * 0.70m;
result += shortStep;
}
else
{
result += lengthOfStep;
}
}

result /= 100;
decimal diff = result / distance*100m;
Console.WriteLine(\$"You travelled {diff:f2}% of the distance!");
}
}
}

Тагове:
0 petrovmitko 145 Точки

Здравей, аз съм на JavaScript, може да погледнеш моето решение без фор цикъл:

При  твоето решение FOR цикъла трябва да започва от 1  и трябва да е  <= на stepsMade

function solve(steps, length, distance){

let fifth = Math.trunc(steps / 5) // -> мат.трънк реже всичко след точката ( 2.34 => 2)

steps = steps - fifth // изваждаш стъпките, които са по-дълги

let shorterLength = length * 0.7

let cm = steps * length + fifth * shorterLength

distance *= 100 // дистанцията я правиш на сантиметри

let sum = cm / distance *  100

console.log(`You travelled \${sum}% of the distance!`);

// нe съм го закръглил до втория знак, за да не те объркам, защото е на js

}

1 danail2003 27 Точки

Благодаря ти!

0 Ivankooo1 9 Точки

Здравей,а това има ли как да се разпише без Math.trunc?

0 krum_43 457 Точки

Здравей колега,

Защо си използвал толкова големи типове данни.Според мен не е необходимо.Освен това може да се мине и без цикъл.

Ето ти едно примерно решение:

https://pastebin.com/qfja0g3x

0
26/02/2020 11:45:38 ivelco 1 Точки

using System;

namespace Distance_Calculator
{
class Program
{
static void Main(string[] args)
{
decimal result = 0;
decimal shortStep = 0;

for (int i = 0; i < stepsMade; i++)  // for (int i = 1; i <= stepsMade; i++)
{
if (i % 5 == 0)
{
shortStep = lengthOfStep * 0.70m;
result += shortStep;
}
else
{
result += lengthOfStep;
}
}

result /= 100;
decimal diff = result / distance*100m;
Console.WriteLine(\$"You travelled {diff:f2}% of the distance!");
}
}
}

Променх ти for цикъла, трябва да почва от 1 до <= stepsMade . Вече дава 100 / 100

for (int i = 1; i <= stepsMade; i++)                                0
26/02/2020 15:24:51