#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;
}