网易2018内推编程题:操作序列(Java实现)

小易有一个长度为n的整数序列,a_1,...,a_n。然后考虑在一个空序列b上进行n次以下操作:
1、将a_i放入b序列的末尾
2、逆置b序列
小易需要你计算输出操作n次之后的b序列。

import java.util.Scanner;
 
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String len = in.nextLine();
        String string = in.nextLine();
        operateArray(string);
    }
 
    public static void operateArray(String a) {
        String[] out = a.split(" ");
        String[] out2 = new String[out.length];
        int l = out.length - 1;
        int f = 0;
        int m = 0;
        if (l % 2 == 0) {
            f = 1;
        }
        if (out.length == 1) {
            System.out.println(out[0]);
        }
 
        for (int i = 0; i < out.length / 2; i++) {
            out2[i] = out[l - f];
            l -= 2;
            m += 1;
        }
        l = 0;
        for (int j = 0; j < out.length / 2 + f; j++) {
            if (j != (out.length / 2 + f)) {
                out2[m + j] = out[l];
 
            }
            l += 2;
        }
        if (out2.length <= 3) {
            for (int n = out2.length - 1; n >= 0; n--) {
                if (n != 0) {
                    System.out.print(out2[n] + " ");
 
                } else {
                    System.out.print(out2[n]);
 
                }
            }
        } else {
            for (int n = 0; n < out2.length; n++) {
                if (n != (out2.length - 1)) {
                    System.out.print(out2[n] + " ");
 
                } else {
                    System.out.print(out2[n]);
 
                }
            }
        }
 
    }
}

猜你喜欢

转载自blog.csdn.net/qq_29519041/article/details/81158798