当前位置:网站首页>The robot is playing an old DOS based game

The robot is playing an old DOS based game

2022-06-25 14:58:00 qq_ twenty-three million nine hundred and fifty-three thousand

Robots are playing an ancient game based on DOS The game of . There is... In the game N+1 building —— from 0 To N Number , From left to right . The number is 0 The height of the building is 0 A unit of , The number is i The height of the building is H(i) A unit of .

At first , The robot is numbered 0 At the building . Each step , It jumps to the next ( On the right ) Architecture . Suppose the robot is in the k A building , And its current energy value is E, Next, it will jump to the k+1 Architecture . It will gain or lose in proportion to H(k+1) And E Energy of difference . If H(k+1) > E Then the robot will lose H(k+1) - E Energy value of , Otherwise it will get E - H(k+1) Energy value of .

The goal of the game is to reach the third N Architecture , In the process , Energy value cannot be negative units . The question now is how much energy the robot starts the game , To ensure the successful completion of the game ?

analysis :
1, If E > H(k + 1) be E = E + (E - H(k + 1))
2, If E < H(k + 1) be E = E - (H(K + 1) - E)
in summary ,E = 2*E - H(K + 1)
The above backward method can be used to solve the problem
#include
#include
using namespace std;

int main()
{
int iNum;
cin >> iNum;
std::vector v;

for(int i = 0; i <iNum; i++)
{
    int iTmp;
    cin >> iTmp;
    v.push_back(iTmp);
}

int iE = 0;
int iTmp = 0;
for(int j = iNum - 1; j >= 0 ; j--)
{
    iTmp = iE +v[j];
    if(iTmp & 0x1)
       iTmp += 1; 
   
    iE = iTmp / 2; 
}
cout <<iE;
return 0;

}

原网站

版权声明
本文为[qq_ twenty-three million nine hundred and fifty-three thousand ]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202200514582320.html