猿辅导22号笔试第一题
有没有大佬帮忙看下代码哪里有问题,一直是0%。
// 第一题
public class YuanFuDao {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] tree = new int[n];
for (int i = 0; i<n; i++){
tree[i] = sc.nextInt();
}
List<Integer> list = new ArrayList<>();
list.add(tree[0]);
// 左边界
int index = 0;
while (2 * index + 1 < n) {
list.add(tree[2 * index + 1]);
index = 2 * index + 1;
}
// 下层边界
int left = index;
index += 1;
for (; index < n; index ++){
list.add(tree[index]);
}
// 次下层边界
index = (index-2)/2 + 1;
while (index < left){
list.add(tree[index]);
index++;
}
// 右边界
index = 0;
while (2 * index + 2 < n) {
list.add(tree[2 * index + 2]);
index = 2 * index + 2;
}
StringBuilder sb = new StringBuilder();
int i = 0;
for (; i<list.size()-2; i++){
sb.append(list.get(i));
sb.append(' ');
}
sb.append(list.get(i));
System.out.println(sb.toString());
}
}
查看9道真题和解析
