You want to roast a few
toasts for the upcoming party. There is a frying pan, that can roast at the
same time k toasts. Roasting the
toast from one side takes 2 minutes. We assume that placing a toast into a
frying pan, turning and removing it from the pan performed instantly. Write a
program that computes the minimum time in minutes for roasting n toasts. Toasts should not be removed
from the pan sooner or later that 2 minutes required for toasting one side.
Each toast should be roasted from both sides.
Input. The first line contains two integers n and k (0 ≤ n ≤ 1000, 1 ≤ k ≤ 50) – the number of toasts and
pan capacity.
Output. Print the minimum time in
minutes required to roast n toasts.
Sample
input |
Sample
output |
3 2 |
6 |
mathematics
Algorithm analysis
If n = 0, the answer is 0.
If the number of toasts n is no
more than pan’s capacity k (n ≤ k), then it is required 4 minutes.
The toasts must be roasted from both sides, so in total we need to roast toast’s sides. Roasting
the toast from one side takes 2 minutes. So if n > k, the number of
minutes to roast n toasts equals to 2
* .
Example
Let there be n
= 3 toasts, k = 2 toasts can be placed in the frying pan. Let’s denote the sides of the toasts as 1a,
1b, 2a, 2b, 3a, 3b.
Then fry all 6
sides in 6 minutes as follows:
·
First roast: 1a 2a – 2 minutes;
·
Second roast: 1b 3a – 2 minutes;
·
Third roast: 2b 3b – 2 minutes;
Algorithm realization
Read input data.
scanf("%d %d",&n,&k);
If n = 0, the answer is 0.
if (!n) res = 0; else
If the number of toasts is no more than pan’s capacity, then we need 4 minutes.
if (n <= k) res = 4; else
{
As we need to roast the toasts from both sides, in total we need to roast toast’s sides. Remember
that one roasting takes 2 minutes.
res = 2 * n / k;
if (2 * n %
k) res++;
res *= 2;
}
Print the answer.
printf("%d\n",res);
Java realization
import java.util.*;
public class Main
{
public static void main(String []args)
{
Scanner con = new
Scanner(System.in);
int n = con.nextInt();
int k = con.nextInt();
int res;
if (n == 0) res = 0; else
if (n <= k) res = 4; else
{
res = 2 * n / k;
if (2 * n % k > 0) res++;
res *= 2;
}
System.out.println(res);
con.close();
}
}