版权声明:iQXQZX https://blog.csdn.net/Cherishlife_/article/details/85240228
哈士奇“跳啊跳”
Time Limit: 1000 ms Memory Limit: 65536 KiB
Problem Description
哈士奇喜欢跳啊跳,现在他站在一条路上,路被分成了 N 段,每段道路的路旁有一个路标:“请往前跳 Xi 步”,哈士奇一开始在第 1 段,如果哈士奇在第 i 段路上,那么他会按照第 i 块路标的指示再往前跳 Xi 步,请问哈士奇能经过终点 N 吗?
Input
第一行包含一个整数 N(1 <= N <= 1000) ,表示路的段数。
第二行包含 N 个用空格隔开的整数 Xi(0 <= Xi <= 1000) ,表示第 i 块路标指示哈士奇需要往前跳的步数。
Output
如果哈士奇能跳到终点 N,输出 “YES”,不能输出 “NO”。
Sample Input
3 1 2 3
Sample Output
YES
Hint
Source
Miracle_QSH
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin >> n;
int a[1111];
for (int i = 1; i <= n; i++)
cin >> a[i];
int i = 1, ans = 0;
while (ans < n - 1)
{
if (a[i] > 0)
{
ans += a[i];
i += a[i];
}
else
break;
}
if (ans >= n - 1)
cout << "YES" << endl;
else
cout << "NO" << endl;
return 0;
}