山鬼

这道题题目意思很简单

刚开始很容易就想到:只要输出n*2/k向上取整就行了

所以就不加思索打出了一下代码:

#include<bits/stdc++.h>
using namespace std;
int n,k;
int main()
{
	cin>>n>>k;
	if(n%k==0)
	cout<<n*2/k;
	else
	cout<<n*2/k+1;
	return 0;
}

  但是,却发现万一 k>n 怎么办

那就输出2就好了

正确代码如下

#include<bits/stdc++.h>
using namespace std;
int n,k;
int main()
{
	cin>>n>>k;
	if(k>=n)
	cout<<2;
	else
	{
		if(n%k==0)
		cout<<n*2/k;
		else
		cout<<n*2/k+1;
	}
	return 0;
}

  还有,这个数据范围真的很恶心

1<=n,k<=10;

让人以为要暴搜

但优化优化就会发现就是上面的思路

 

原文地址:https://www.cnblogs.com/xsxbqsjxj/p/11182044.html