LakerV最近沉迷于微信的跳一跳无法自拔。为了戒掉自己的网瘾,他决定去做算法题了。刚好,他看到一个题目叫“跳一跳”的算法题,决定点开去做。
题目的描述是这样的,有n块瓷砖铺成的直线道路上,你一开始站在第一块瓷砖上,每一块瓷砖都有一个ai权值表示当你到达这块瓷砖后,你的下一步最远能到达i+ai这个瓷砖。现在问你最大能到达第几块瓷砖
LakerV认为这道题特别简单,于是扔给你来做,自己去做别的了。
#include <bits/stdc++.h>
#include <algorithm>
#include <cmath>
using namespace std;
int main ()
{
int n;
int ans=1,x;
cin >>n;
for (int i =1;i<=n;i++)
{
cin>>x;
if (i>ans)//超出可以跳到的最远值
break;
if (i<=n)//未结束时
ans =max(ans,i+x);//ans最大值
}
if (ans>=n)
cout <<n;
else
cout <<ans;
return 0;
}