首页 > 2021招银秋招java最后一道编程题
头像
冰与火之歌201908192037329
发布于 2020-06-22 22:08
+ 关注

2021招银秋招java最后一道编程题

public class Solution2 {

    //7 2 1 2 3 4 5 6 7
    static int[] cards;

    @SuppressWarnings("all")
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] s = br.readLine().split(" ");
        int n = Integer.parseInt(s[0]);
        int k = Integer.parseInt(s[1]);
        cards = new int[s.length - 2];
        for (int i = 0; i < cards.length; i++) {
            cards[i] = Integer.parseInt(s[i + 2]);
        }


        for (int m = 0; m < 2; m++) {
            int[] temp = new int[cards.length];
            int i = n % 2 == 0 ? n / 2 - 1 : n / 2;
            int j = cards.length - 1;
            int idx = cards.length - 1;
            if (m % 2 == 0) {//奇数
                while (idx >= 0 && i >= 0 && j >= 0) {

                    if (idx > 0){
                        temp[idx--] = cards[j--];
                        temp[idx--] = cards[i--];
                    }
                    else if (idx == 0)
                        temp[idx] = cards[i--];
                }

            }else {
                while (idx >= 0 && i >= 0 && j >= 0) {

                    if (idx > 0){
                        temp[idx--] = cards[i--];
                        temp[idx--] = cards[j--];
                    }
                    else if (idx == 0)
                        temp[idx] = cards[i--];
                }

            }
            cards = temp;
        }

        for (int card : cards) {
            System.out.print(card + " ");
        }
    }
}

全部评论

(0) 回帖
加载中...
话题 回帖

推荐话题

相关热帖

近期热帖

历年真题 真题热练榜 24小时
技术(软件)/信息技术类
查看全部

近期精华帖

热门推荐