这道题目开始提交多次都出错,后来试了一下就A了,也没对代码进行什么改变,非常费解。还想给UVa提一个建议,为什么不能看到自己提交以后的代码?如果像POJ可以看的话,对自己对比自己的代码,找到错误很有帮助!
还要注意一点的是:判断Success还是Failure的时候,是当前高度height > heightOfWell,height < 0, 而不是 <=, >=. 题目中的原句是 In other words, the snail's height will exceed the height of the well or become negative
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LOCAL
float h, u, d, f;// h - height, u - distance climb, d - slide, f - fatigue
int main()
{
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
scanf("%f%f%f%f", &h, &u, &d, &f);
while (h != 0)
{
float speed = u, h_t = 0, delta = u * f/100.0;
int iter = 0, flagSuccess = 0, flagFail = 0;
while ((!flagFail)&&(!flagSuccess))
{
iter++;
speed = (speed > 0)?speed:0;
if (h_t + speed > h)
flagSuccess = 1;
else if (h_t + speed - d < 0)
flagFail = 1;
h_t = h_t + speed - d;
speed -= delta;
}
if (flagFail)
printf("failure on day %d\n", iter);
if (flagSuccess)
printf("success on day %d\n", iter);
scanf("%f%f%f%f", &h, &u, &d, &f);
}
return 0;
}