Problem P24. [算法课贪心] 跳跃游戏
 给定一个非负整数数组 nums ,你最初位于数组的 第一个下标 。
数组中的每个元素代表你在该位置可以跳跃的最大长度
判断你是否能够到达最后一个下标。
输入
输入一行数组nums
输出
输出true/fasle
样例
标准输入
 2 3 1 1 4
 标准输出
 true
 标准输入
 3 2 1 0 4
 标准输出
 false
解题思路

参考文章
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
bool game(vector<int> &arr)
{
    int n = arr.size();
    int max_pos = 0; // 记录到达的最大位置,初始位置0,数组第一位
    if(n <= 1) // 数组只有一个数字,即到达了
        return true;
    for(int i=0; i<n-1; i++) // 遍历数组
    {
        if(i <= max_pos) // 如果数组当前的位置比到达的最大位置大,说明到不了该位置,就是false
        {
            max_pos = max(max_pos, i+arr[i]); // i位置一定可以到达,此时选此位置跳跃和目前的max位置进行比较,再取max
            if(max_pos >= n-1) // 跳出数组,则ok
            {
                return true;
            }
        }
        else
        {
            return false;
        }
    }
}
int main()
{
    int n;
    vector<int> arr;
    while(cin >> n) // 数组中出现0则
    {
//        if(n == 0)
//        {
//            cout << "false";
//            return 0;
//        }
//        else
        {
            arr.push_back(n);
        }
    }
    bool flag = game(arr);
    if(flag)
    {
        cout << "true";
    }
    else
    {
        cout << "false";
    }
//    cout << "Hello world!" << endl;
    return 0;
}



















