恶魔猎手尤迪安野心勃勃,他背叛了暗夜精灵,率领深藏在海底的娜迦族企图叛变。守望者在与尤迪安的交锋中遭遇了围杀,被困在一个荒芜的大岛上。为了杀死守望者,尤迪安开始对这个荒岛施咒,这座岛很快就会沉下去。到那时,岛上的所有人都会遇难。守望者的跑步速度为17m/s,以这样的速度是无法逃离荒岛的。庆幸的是守望者拥有闪烁法术,可在1s内移动60m,不过每次使用闪烁法术都会消耗魔法值10点。守望者的魔法值恢复的速度为4点/s,只有处在原地休息状态时才能恢复。 现在已知守望者的魔法初值M,他所在的初始位置与岛的出口之间的距离S,岛沉没的时间T。你的任务是写一个程序帮助守望者计算如何在最短的时间内逃离荒岛,若不能逃出,则输出守望者在剩下的时间内能走的最远距离。注意:守望者跑步、闪烁或休息活动均以秒(s)为单位,且每次活动的持续时间为整数秒。距离的单位为米(m)。
【输入格式】空格隔开的三个非负整数M, S, T。
【输出格式】 第1行为字符串“Yes”或“No”,即守望者是否能逃离荒岛。 第2行包含一个整数。第一行为“Yes"时表示守望者逃离荒岛的最短时间;第一行为“No”时表示守望者能走的最远距离。
#include <stdio.h> //参数分别为魔法,距离,时间,能都成逃脱,花费的时间或者最短时间 void run(int magic,int distant,int time,bool &canRun,int &timeOrDistant) { int nowDistant=magic/10*60;//使用完魔法跑的距离 int nowMagic=magic%10;//现在的魔法 int remainingTime=time-magic/10;//剩余时间 //是否用尽魔法之前是否就成功逃出了 while(nowDistant>=distant) { //是否就成功出逃了 if(nowDistant-60>=distant) { remainingTime++; nowDistant-=60; } } //用尽魔法刚好逃脱 if(nowDistant>=distant) { canRun=true; timeOrDistant=time-remainingTime; return ; } //尝试是原地休息还是跑 int tempTime=remainingTime; while(--tempTime>0) { //如果再跑一秒就逃脱 if(nowDistant+17>=distant) { canRun=true; timeOrDistant=time-tempTime; return ; } //否则原地停一秒 else nowMagic+=4; if((nowMagic)/10) { nowDistant+=60; remainingTime=--tempTime; if(nowDistant>=distant) { canRun=true; timeOrDistant=time-tempTime; return ; } } } //回复不了10点魔法,剩余的时间全部跑 while(remainingTime--) { nowDistant+=17; if(nowDistant>=distant) { canRun=true; timeOrDistant=time-remainingTime; return ; } } //死定了 canRun=false; timeOrDistant=nowDistant; } int main() { bool canRun; int timeOrDistant; int M,T,D; while(scanf("%d%d%d",&M,&D,&T)) { run(M,D,T,canRun,timeOrDistant); if(canRun) printf("Yes\n%d\n",timeOrDistant); else printf("No\n%d\n",timeOrDistant); } return 0; }