给定一个非负整数数组,假定你的初始位置为数组第一个下标。
数组中的每个元素代表你在那个位置能够跳跃的最大长度。
请确认你是否能够跳跃到数组的最后一个下标。
例如:
A = [2,3,1,1,4],
return true.
A = [3,2,1,0,4],
return false.
格式:
第一行输入一个正整数n,接下来的一行,输入数组A[n]。如果能跳到最后一个下标,输出“true”,否则输出“false”
样例输入
5
2 0 2 0 1
样例输出
true
===============================================
第一次code:
1 import java.util.Scanner; 2 3 public class Main 4 { 5 public static void main(String[] args) 6 { 7 @SuppressWarnings("resource") 8 Scanner input = new Scanner(System.in); 9 int a =input.nextInt(); 10 int[] list = new int [a]; 11 for(int i = 0; i < a ; i++) 12 { 13 list[i] = input.nextInt(); 14 } 15 String str = "true" ; 16 int temp = list[0]; 17 while( temp < a ) 18 { 19 temp += list[temp]; 20 if( temp == a-1) 21 { 22 break; 23 } 24 if(list[temp] == 0 && temp != (a - 1)) 25 { 26 str="false"; 27 break; 28 } 29 } 30 System.out.println(str); 31 } 32 }