猿辅导笔试8.22,
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
Map<Integer, Integer> m = new HashMap<>();
for (int i = 1; i <= n; i++) {
m.put(i, sc.nextInt());
}
List<Integer> result = new ArrayList<>();
Set<Integer> visited = new HashSet<>();
int level = 0;
while(Math.pow(2, level)<=n){
result.add((int) Math.pow(2, level));
visited.add((int) Math.pow(2, level));
level++;
}
level-=1;
while (level>0){
int start = (int) Math.pow(2, level);
int end = (int) Math.pow(2, level+1)-1;
for (int i = start; i <= end && i<=n; i++) {
if (i == end){
result.add(i);
visited.add(i);
continue;
}
if (!visited.contains(i) && !visited.contains(2*i) && !visited.contains(2*i+1)){
result.add(i);
visited.add(i);
}
}
level--;
}
List<Integer> realResult = new ArrayList<>();
for (int index:result){
realResult.add(m.get(index));
}
for (int i = 0; i < realResult.size(); i++) {
System.out.print(realResult.get(i));
if (i!=realResult.size()-1){
System.out.print(" ");
}
}
}
}
第一题
本地测试没问题,提交上去一直报ac 0%,啥子问题,有点郁闷
#猿辅导##笔试题型#
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
Map<Integer, Integer> m = new HashMap<>();
for (int i = 1; i <= n; i++) {
m.put(i, sc.nextInt());
}
List<Integer> result = new ArrayList<>();
Set<Integer> visited = new HashSet<>();
int level = 0;
while(Math.pow(2, level)<=n){
result.add((int) Math.pow(2, level));
visited.add((int) Math.pow(2, level));
level++;
}
level-=1;
while (level>0){
int start = (int) Math.pow(2, level);
int end = (int) Math.pow(2, level+1)-1;
for (int i = start; i <= end && i<=n; i++) {
if (i == end){
result.add(i);
visited.add(i);
continue;
}
if (!visited.contains(i) && !visited.contains(2*i) && !visited.contains(2*i+1)){
result.add(i);
visited.add(i);
}
}
level--;
}
List<Integer> realResult = new ArrayList<>();
for (int index:result){
realResult.add(m.get(index));
}
for (int i = 0; i < realResult.size(); i++) {
System.out.print(realResult.get(i));
if (i!=realResult.size()-1){
System.out.print(" ");
}
}
}
}
第一题
本地测试没问题,提交上去一直报ac 0%,啥子问题,有点郁闷
#猿辅导##笔试题型#