zoukankan      html  css  js  c++  java
  • 计蒜客 挑战难题 跳跃游戏

    给定一个非负整数数组,假定你的初始位置为数组第一个下标。

    数组中的每个元素代表你在那个位置能够跳跃的最大长度。

    请确认你是否能够跳跃到数组的最后一个下标。

    例如:

    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 }
     
  • 相关阅读:
    学习增删查改
    学习MVC
    Milkyway 用的不规则LEF
    innovus 自学之 摆放数模接口port
    innovus 自学之 一些小命令
    innovus 自学小技巧之 gui小配置
    innovus 自学小技巧之floorplan
    第二周 第四节pyc是什么
    第二周 第三节初识模块2
    第二周第二节模块初识
  • 原文地址:https://www.cnblogs.com/niithub/p/5991056.html
Copyright © 2011-2022 走看看