`
xidajiancun
  • 浏览: 454496 次
文章分类
社区版块
存档分类
最新评论

ZOJ 1494 Climbing Worm 数学水题

 
阅读更多

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=494

题目大意:

一只蜗牛要从爬上n英寸高的地方,他速度为u每分钟,他爬完u需要休息1分钟,且他休息时下滑d英寸,问他什么时候爬出去。

吐槽:

小学的数学题编程了编程题,简直丧心病狂。

思路:

数据量小直接模拟即可。

也可以用数学推导


模拟板:

#include<cstdio>

int main()
{
	int n,u,d;
	while(~scanf("%d%d%d",&n,&u,&d),n||u||d)
	{
		int h=0;
		int ans=0;
		while(h<n)
		{
			ans++;
			h+=u;
			if(h >=n)
				break;
			ans++;
			h-=d;
		}
		printf("%d\n",ans);
	}
	return 0;	
}


公式版:

#include<cstdio>

int main()
{
	int n,u,d;
	while(~scanf("%d%d%d",&n,&u,&d),n||u||d)
	{
		int h=0;
		double temp=double(n-d)/double(u-d);
		int ans;
		if(temp - (int) temp< 1e-12)
			ans=temp;
		else
			ans=temp+1;

		printf("%d\n",2*ans-1);
	}
	return 0;	
}


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics