面试题

问题:一副从1到n的牌,每次从牌堆顶取一张放桌子上,再取一张放牌堆底,直到牌堆没牌,最后桌子上的牌是从1到n有序,设计程序,输入n,输出牌堆的顺序数组

public static void MS(int n){
        ArrayList ints = new ArrayList<>();
        ArrayList ints2 = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            ints.add(i+1);
        }
        for (int i = 0; i < ints.size(); i++) {
            if(i%2 != 0){
                ints.add(ints.get(i));
            }
        }
        for (int i = 0; i < ints.size(); i++) {
            if(i%2 ==0){
                ints2.add(ints.get(i));
            }
            if(ints2.size() == n)
                break;
        }


        for (int i = 0; i < n; i++) {
            for (int j = 0; j < ints2.size(); j++) {
                if(ints2.get(j) == i+1){
                    System.out.println(j+1);
                }
            }
        }
    }

你可能感兴趣的:(面试题)