你们的方法都好高端,我只想到了用链表的方式来解决这个问题
意外的内存和速度都比较好
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
ListNode head=new ListNode(sc.nextInt());
ListNode foot=head;
if(n%2==0){
for(int i=1;i<n;i++){
ListNode temp=new ListNode(sc.nextInt());
if(i%2==0){
foot.next=temp;
foot=temp;
}else{
temp.next=head;
head=temp;
}
}}else{
for(int i=1;i<n;i++){
ListNode temp=new ListNode(sc.nextInt());
if(i%2==1){
foot.next=temp;
foot=temp;
}else{
temp.next=head;
head=temp;
}
}
}
for(int i=0;i<n;i++){
System.out.print(head.date);
System.out.print(" ");
head=head.next;
}
}
}
class ListNode{
public int date;
public ListNode next;
ListNode(int da){
this.date=da;
}
}
京公网安备 11010502036488号