The Snail

The Snail

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2513    Accepted Submission(s): 1808


Problem Description
A snail is at the bottom of a 6-foot well and wants to climb to the top. The snail can climb 3 feet while the sun is up, but slides down 1 foot at night while sleeping. The snail has a fatigue factor of 10%, which means that on each successive day the snail climbs 10% * 3 = 0.3 feet less than it did the previous day. (The distance lost to fatigue is always 10% of the first day's climbing distance.) On what day does the snail leave the well, i.e., what is the first day during which the snail's height exceeds 6 feet? (A day consists of a period of sunlight followed by a period of darkness.) As you can see from the following table, the snail leaves the well during the third day. 

Day Initial Height Distance Climbed Height After Climbing Height After Sliding 
1 0 3 3 2 
2 2 2.7 4.7 3.7 
3 3.7 2.4 6.1 - 

Your job is to solve this problem in general. Depending on the parameters of the problem, the snail will eventually either leave the well or slide back to the bottom of the well. (In other words, the snail's height will exceed the height of the well or become negative.) You must find out which happens first and on what day.
 

Input
The input file contains on e or more test cases, each on a line by itself. Each line contains four integers H, U, D, and F, separated by a single space. If H = 0 it signals the end of the input; otherwise, all four numbers will be between 1 and 100, inclusive. H is the height of the well in feet, U is the distance in feet that the snail can climb during the day, D is the distance in feet that the snail slides down during the night, and F is the fatigue factor expressed as a percentage. The snail never climbs a negative distance. If the fatigue factor drops the snail's climbing distance below zero, the snail does not climb at all that day. Regardless of how far the snail climbed, it always slides D feet at night.
 

Output
For each test case, output a line indicating whether the snail succeeded (left the well) or failed (slid back to the bottom) and on what day. Format the output exactly as shown in the example.
 

Sample Input
  
  
6 3 1 10 10 2 1 50 50 5 3 14 50 6 4 1 50 6 3 1 1 1 1 1 0 0 0 0
 

Sample Output
  
  
success on day 3 failure on day 4 failure on day 7 failure on day 68 success on day 20 failure on day 2
题目很简单,第一次做居然没过,忽视了刚好爬到井口不算出去。。。
#include <stdio.h>
#include <stdlib.h>

int main()
{
    double h,u,d,f,m;
    while(scanf("%lf%lf%lf%lf",&h,&u,&d,&f)==4)
    {
        if(h==0)break;
        m=u;
        double s=0;
        int i,n=0;
        for(i=1;;i++)//按照蜗牛爬行的过程写个循环
        {
            s=s+u;
            if(s>h)//不用等
            {
                n=1;
                break;
            }
            s=s-d;//白天没出去,晚上就要下滑
            if(s<0)
            {
                n=0;
                break;
            }
            u=u-0.01*f*m;
        }
        if(n==0)
        {
            printf("failure on day %d\n",i);
        }
        if(n==1)
        {
            printf("success on day %d\n",i);
        }
    }
    return 0;
}
### 关于有限状态机的概念 有限状态机(Finite State Machine, FSM),是一种表示对象行为的方式,该方式基于一组有限数量的状态以及这些状态之间的转移条件。FSM广泛用于计算机科学领域,特别是在编译原理、自然语言处理和游戏开发等方面。 在软件工程实践中,Spring Statemachine是一个强大的工具,它能够帮助开发者高效地管理诸如订单等业务流程中的状态流转逻辑[^2]。通过简单的配置和灵活的状态转换定义,可以实现复杂的状态控制。下面将以“开心的蜗牛”为例展示如何利用Spring Statemachine构建一个简单而有趣的有限状态机应用。 ### 使用 Spring Statemachine 实现 '开心的蜗牛' #### 定义状态与事件 对于名为“开心的蜗牛”的案例来说,假设存在三种基本状态:`SLEEPING`(睡觉), `EATING`(吃东西),以及`PLAYING`(玩耍)。同时有四个触发状态变化的动作或事件:`wakeUp`, `eatFood`, `startPlay`, 和 `goToSleep`. ```java public enum States { SLEEPING, EATING, PLAYING; } public enum Events { WAKE_UP, EAT_FOOD, START_PLAY, GO_TO_SLEEP; } ``` #### 配置状态迁移规则 接下来,在应用程序启动类中设置好各个状态下允许发生的转变: ```java @Configuration @EnableStateMachineFactory public class Config extends EnumeratedStateMachineConfigurerAdapter<States, Events> { @Override public void configure(StateMachineStateConfigurer<States, Events> states) throws Exception { states.withStates() .initial(States.SLEEPING) .states(EnumSet.allOf(States.class)); } @Override public void configure(StateMachineTransitionConfigurer<States, Events> transitions) throws Exception { transitions.withExternal().source(States.SLEEPING).target(States.EATING).event(Events.WAKE_UP) .and().withExternal().source(States.EATING).target(States.PLAYING).event(Events.START_PLAY) .and().withExternal().source(States.PLAYING).target(States.SLEEPING).event(Events.GO_TO_SLEEP); } // ... other configurations ... } ``` 这段代码片段描述了一个非常基础的状态图结构,其中包含了从一种状态到另一种状态的变化路径. #### 控制器层交互设计 为了使这个例子更加生动有趣,可以在控制器层面加入一些人性化的接口方法供外部调用者操作这只虚拟的小蜗牛: ```java @RestController @RequestMapping("/snail") public class SnailController { private final StateMachine<States, Events> stateMachine; @Autowired public SnailController(StateMachine<States, Events> stateMachine){ this.stateMachine = stateMachine; } @PostMapping("/wake-up") public String wakeUp(){ stateMachine.sendEvent(Events.WAKE_UP); return "The snail has woken up!"; } @PostMapping("/eat-food") public String eatFood(){ stateMachine.sendEvent(Events.EAT_FOOD); return "The snail is eating now."; } @PostMapping("/play") public String play(){ stateMachine.sendEvent(Events.START_PLAY); return "The snail starts playing happily."; } @PostMapping("/sleep") public String sleep(){ stateMachine.sendEvent(Events.GO_TO_SLEEP); return "Good night! The snail goes to bed."; } } ``` 以上就是使用Spring Statemachine框架创建的一个简易版“开心的蜗牛”有限状态机实例说明。希望这能给读者带来启发并激发更多创意性的应用场景探索.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值