小易有一个长度为 n 的整数序列,,...,。然后考虑在一个空序列 b 上进行 n 次以下操作:
1、将 放入 b 序列的末尾
2、逆置 b 序列
小易需要你计算输出操作 n 次之后的 b 序列。
输入包括两行,第一行包括一个整数n(2 ≤ n ≤ 2*10^5),即序列的长度。 第二行包括n个整数 ai (1 ≤ ai ≤ 10^9),即序列a中的每个整数,以空格分割。
在一行中输出操作 n 次之后的 b 序列,以空格分割,行末无空格。
4 1 2 3 4
4 2 1 3
按照题目的意思可以这样写
使用一个LinkedList保存数据,按照顺序分别从后和从前插入数据,之后按照插入数据的总数量,从前或者从后遍历输出就行了。
import java.util.*; public class Main{ public static void main(String[] args){ Scanner s= new Scanner(System.in); int len = s.nextInt(); LinkedList<Integer> list = new LinkedList<Integer>(); for(int i=0;i<len;){ list.add(s.nextInt()); i++; if(i<len){ list.addFirst(s.nextInt()); i++; } } s.close(); Iterator itr; if(len%2==0) itr = list.iterator(); else itr = list.descendingIterator(); if(itr.hasNext())System.out.print(itr.next()); while(itr.hasNext()){ System.out.print(" "+itr.next()); } } }
import java.util.ArrayList; import java.util.Scanner; import java.util.Stack; public class Main { public static void main(String argc[]){ Scanner in = new Scanner(System.in); int n =in.nextInt(); int[] num =new int[n]; for(int i=0;i<n;i++) num[i]=in.nextInt(); ArrayList<Integer> result=myfun(num); for(int i=0;i<result.size();i++){ System.out.print(result.get(i)); System.out.print(" "); } } private static ArrayList<Integer> myfun(int[] num){ ArrayList<Integer> result = new ArrayList<>(num.length); Stack<Integer> stack = new Stack<>(); int i=0; while(true){ result.add(num[i]); for (int j=0;j<result.size();j++){ stack.push(result.get(j));} result.clear(); while (!stack.isEmpty()) result.add(stack.pop()); i++; if(result.size()==num.length) break; } return result; } }
import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); long[] array = new long[n]; for(int i = 0; i < n; i++) { array[i] = scanner.nextLong(); } if(n == 1) { System.out.println(1); return; } if(n == 2) { System.out.print("2 1"); return; } List<Integer> result = new ArrayList<>(n); if(n % 2 == 0) { int tmp1 = n; while(tmp1 != 2) { result.add(tmp1); tmp1 -= 2; } result.add(2); int tmp2 = 1; while(tmp2 < n) { result.add(tmp2); tmp2 += 2; } } else { int tmp1 = n; while(tmp1 != 1) { result.add(tmp1); tmp1 -= 2; } result.add(1); int tmp2 = 2; while(tmp2 < n) { result.add(tmp2); tmp2 += 2; } } StringBuffer sb = new StringBuffer(); result.stream().forEach(i -> sb.append(array[i - 1] + " ")); System.out.print(sb.toString().trim()); } }
import java.util.*; public class Main{ public static void main(String[] args){ try(Scanner in = new Scanner(System.in)){ in.nextLine(); String[] s = in.nextLine().split(" "); int[] a = new int[s.length]; for(int i = 0;i < a.length;i++){ a[i] = Integer.parseInt(s[i]); } int[] res = helper(a); int i = 0; for(int num : res){ System.out.print(num); if(i == res.length) System.out.println(); else System.out.print(" "); } } } public static int[] helper(int[] a){ int[] res = new int[a.length]; int i = 0,j = res.length - 1,k = a.length - 1; while(i <= j){ res[i++] = a[k--]; if(k > 0) res[j--] = a[k--]; } return res; } }
public class test4 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String h = scanner.nextLine(); String s=scanner.nextLine(); String[] arr = s.split("\\s+"); int j=arr.length-1; int i; if(arr.length%2==0){i=0;}//对于奇数和偶数的操作 else i=1; //是偶数的操作 for (int k=0;k<arr.length;k++) { if (j >=0) { System.out.print(arr[j]+" "); j = j-2; } if (j<0&&i<arr.length&&i!=arr.length-2){ System.out.print(arr[i]+" "); i=i+2; } if (j<0&&i<arr.length&&i>=arr.length-2){ System.out.print(arr[i]); i=i+2; } } } }
import java.util.Scanner; public class Main{ public static void main(String[] args) { int[][] a = new int[2][100000]; int n; Scanner sc = new Scanner(System.in); n = sc.nextInt(); int q = 1; int l = 0, r = 0; for (int i = 0; i < n; i++) { if (q == 0) { a[0][l++] = sc.nextInt(); } else { a[1][r++] = sc.nextInt(); } q = q ^ 1; } for (int i = ((q != 1) ? (r - 1) : (l - 1)); i >= 0; i--) System.out.print(a[q ^ 1][i]+" "); for (int i = 0; i < ((q != 1) ? l : r); i++) { System.out.print(a[q][i]); if(i != ((q != 1) ? l : r) - 1) System.out.print(" "); } sc.close(); } }