题目描述
In China, there is a very famous problem about pancakes: You have a pan and you can fry two pancakes at the same time each time. For a pancake, its front and back sides need to be cooked, and it takes one hour for each side to be cooked.
So how long does it take at least to cook 3 pancakes? The answer is three hours:
In the first hour, fry the front of No.1 pancake and the front of No.2 pancake.
In the second hour, fry the back of No.2 pancake and the front of No.3 pancake.
In the third hour, fry the back of No.1 pancake and the back of No.3 pancake.
Now you have a pan and you can fry K pancakes at the same time each time. How many hours does it takes at least to cook N pancakes?
It’s noticed that you have to fry some side of the pancake until fully cooked every time, it means that you can’t fry some side of the pancake half-cooked and taking it out. So the answers are always integers.
输入
The first line has two integers N, K.
1≤N, K≤100
输出
Output the answer.
样例输入
3 2
样例输出
3
题意:经典的小学烙馅饼问题
给定你馅饼的总数量以及每次最多可以烙的数量,馅饼的正反两面都要烙,每次烙一面的时间为一小时,问最少可以多长时间全部烙完
思路:如果馅饼的数量小于锅的盛放数量,则答案一定为2
若大于则继续分情况讨论,对于馅饼的正反两面都要烙,所以可以直接想成2n,如果2n可以正好被k整除,则说明对于每次烙馅饼都可以装满锅,不空不漏,则答案直接为n/k2,反之,若不能整除,则说明最后的时候一定会有锅装不满的情况,所以最终答案为n/k2+1
AC代码
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long ll;
const int N=100010;int n,k; int main()
{
cin>>n>>k;if(n<=k) cout<<2<<endl;else{
if(n%k==0) cout<<n/k*2<<endl;else{
if(2*n%k==0) cout<<n*2/k<<endl;else cout<<2*n/k+1<<endl;}}return 0;
}