华为2018年4月实习笔试题2 数字跳跃


第一行表示有多少个数n
第二行开始依次是1到n个数,一个数一行
输出描述:
输出一行,表示最少跳跃的次数。
示例1
输入
7
2
3
2
1
2
1
5
输出
3
 说明
 7表示接下来要输入7个正整数,从2开始。数字本身代表可以跳跃的最大步长,此时有2种跳法,为2-2-2-5和2-3-2-5都为3步

  1 import java.util.ArrayList;
  2 import java.util.Scanner;
  3 
  4 
  5 public class test2 {
  6 
  7     public static void main(String[] args) {
  8         // TODO Auto-generated method stub
  9         // 第一行表示有多少个数n
 10         // 第二行开始依次是1到n个数,一个数一行
 11         // 输出描述:
 12         // 输出一行,表示最少跳跃的次数。
 13         // 示例1
 14         // 输入
 15         // 7
 16         // 2
 17         // 3
 18         // 2
 19         // 1
 20         // 2
 21         // 1
 22         // 5
 23         // 输出
 24         // 3
 25         // 说明
 26         // 7表示接下来要输入7个正整数,从2开始。数字本身代表可以跳跃的最大步长,此时有2种跳法,为2-2-2-5和2-3-2-5都为3步
 27         Scanner sc = new Scanner(System.in);
 28         int n0 = sc.nextInt();
 29         
 30         ArrayList<Integer> arr = new ArrayList<Integer>();
 31   
 32         int i = 0;
 33         if (n0 ==0) {
 34             System.out.println(0);
 35         } else {
 36             while (i < n0) {
 37                 int next = sc.nextInt();
 38           
 39                 arr.add(next);
 40                 i++;
 41             }
 42         }
 43         
 49         int index = 0;
 50         int n = 0;
 51        
 70         if (n0 == 1) {
 71             System.out.println(0);
 72         } else if (n0 == 2) {
 73             System.out.println(1);
 74         } else {
 75             while (arr.size()  > (index + arr.get(index))) {
 76                 ArrayList<Integer> newarr = getnewarr(arr, index);
 77                 int max = getmax(newarr);
 78                 index = geti(arr, index, max)+1;
 79                 n++;
 80             } 
 81 
 82         }
 83         System.out.println(n);
 84     }
 85 
 86     public static int geti(ArrayList<Integer> arr, int index, int max) {
 87 
 88         int desc = maxindex(arr, max) - index;
 89         if (desc < arr.get(index) && desc > 0) {
 90             return maxindex(arr, max);
 91         } else
 92             return index + arr.get(index);
 93     }
 94 
 95     public static ArrayList<Integer> getnewarr(ArrayList<Integer> arr, int index) {
 96         ArrayList<Integer> newarr = new ArrayList<Integer>();
 97         for (int i = index; i <= index + arr.get(index); i++) {
 98             newarr.add(arr.get(i));
 99         }
100         return newarr;
101     }
102 
103     public static int maxindex(ArrayList<Integer> arr, int max) {
104         int index = 0;
105         for (int i = 0; i < arr.size(); i++) {
106             if (arr.get(i) == max) {
107                 index = i;
108             }
109         }
110         return index;
111     }
112 
113     public static int getmax(ArrayList<Integer> newarr) {
114         int max = 0;
115         for (int i = 0; i < newarr.size(); i++) {
116             if (newarr.get(i) >= max) {
117                 max = newarr.get(i);
118             }
119         }
120         return max;
121     }
122 
123 }

猜你喜欢

转载自www.cnblogs.com/ncznx/p/9161499.html