华为编程大赛--洞穴逃生


#include <iostream>
#include <string>

int main()
{
	//魔法M,距离S,时间T
	int M, S, T;
	std::cin>>M>>S>>T;
	if (S==0)
	{
		std::cout<<"Yes "<<0;
	}
	if (T==0 && S != 0)
	{
		std::cout<<"No "<<0;
	}
	int sEscape = 0;
	//int tUse = 0;
	int tRemain = T;

	//若魔法值大于等于2,使用魔法才是最快的
	while(M >= 2)
	{
		//判断是否还有时间以及是否已经逃出
		if (tRemain >= 0)
		{
			if (sEscape >= S)
			{
				std::cout<<"Yes "<<T -tRemain;
				break;
			}
		}
		else
		{
			std::cout<<"No "<<sEscape;
		}
		//魔法大于10的情况
		if (M >= 10)
		{
			if (tRemain >= 1)
			{
				sEscape += 60;
				tRemain -= 1;
				M -= 10;
			}
			else 
			{
				break;
			}
		}
		//魔法大于6的情况
		else if (M >= 6)
		{
			if (tRemain >= 2)
			{
				sEscape += 60;
				tRemain -= 2;
				M = M + 4 - 10;
			}
			else
			{
				break;
			}
		}
		else
		{
			if (tRemain >= 3)
			{
				sEscape += 60;
				tRemain -= 3;
				M = M + 2 * 4 - 10;
			}
			else 
			{
				break;
			}
		}
		
	}

	//魔法值不足
	while (tRemain > 0 && sEscape < S)
	{
		tRemain -= 1;
		sEscape += 17;
		if (sEscape >= S)
		{
			std::cout<<"Yes "<<T -tRemain;
			break;
		}
		
	}
	//没有剩余时间且未到达出口
	if (tRemain <= 0 && sEscape < S)
	{
		std::cout<<"No "<<sEscape;
	}

	system("pause");
}

网上看到更简洁的答案:

#include <iostream>
using std::endl;
using std::cin;
using std::cout;
int main()
{
  int M,S,T;
  cin >> M >> S >> T;
  int distance=0;
  int flashdistance=0;
  int i;
  if(S==0)
  {
    cout << "Yes " << 0 << endl;
  }else if(T==0 && S!=0)
  {
    cout << "No " << 0 << endl;
  }else{
    for(i=1;i<=T;++i)
    {
      distance+=17;
      if(M<10)
      {
        M+=4;
      }else{
        flashdistance+=60;
        M-=10;
      }
      if(distance<flashdistance)
        distance = flashdistance;
      if(distance >= S)
        break;
    }
    if(i<=T)
      cout << "Yes " << i << endl;
    else
      cout << "No " << distance << endl;
  }
  return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值