zoj1494 暴力模拟 简单数学问题

Climbing Worm

Time Limit: 2 Seconds      Memory Limit:65536 KB

An inch worm is at the bottom of a well n inches deep. It has enough energy to climb u inches every minute, but then has to rest a minute before climbing again. During the rest, it slips down d inches. The process of climbing and resting then repeats. How long before the worm climbs out of the well? We'll always count a portion of a minute as a whole minute and if the worm just reaches the top of the well at the end of its climbing, we'll assume the worm makes it out.


Input

There will be multiple problem instances. Each line will contain 3 positive integers n, u and d. These give the values mentioned in the paragraph above. Furthermore, you may assume d < u and n < 100. A value of n = 0 indicates end of output.


Output

Each input instance should generate a single integer on a line, indicating the number of minutes it takes for the worm to climb out of the well.


Sample Input

10 2 1
20 3 1
0 0 0


Sample Output

17
19

题目大意:

一只蜗牛要从爬上n英寸高的地方,他速度为u每分钟,他爬完u需要休息1分钟,且他休息时下滑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
",ans);
	}

当数据大起来时,不妨想想公式。

个人思路,感谢指点纠正。

1:答案肯定是奇数(因为第一次是向上,最后一次也是向上,当然中间向下是偶数次)。

2:第一次单出来,后面以一上爬加一下滑为一个单位。


如 10 3 1                                            上行       汇总

                第一天:                              3           3

               第二,三天                            2           5

               第四,五天                            2           7

               第六,七天                            2           9

               第八,九天                            2           11(到达)

因为2*n和2*n+1(2*n-1还未到达)是先下后上,所以不会出现2*n就到到的情况。

#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<math.h>
#include<string>
using namespace std;
int a,b,c;
void _solve(){
	int ans;
	ans=1;
	a-=b;
	if(a<0) {cout<<ans<<endl;return ;}//第一天结束 
	int temp;
	if(a%(b-c)==0) temp=a/(b-c);
	else temp=a/(b-c)+1;//尾巴任然算一个单位 
	temp*=2;//除了第一天,后面两天为一个单位 
	ans+=temp;
	cout<<ans<<endl;return ;
}
int main()
{
     while(cin>>a>>b>>c)
       if(!a&&!b&&!c) return 0;
       else _solve();
	 return 0;
}



原文地址:https://www.cnblogs.com/hua-dong/p/7603967.html