大巴车分组,逆序输出

大巴车(数组分块,按块翻转,块内不变)

http://www.nowcoder.com/questionTerminal/a776dfef53914c23a4271fc5fe608369

import java.util.*;
public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        List<List<Integer>> r = new ArrayList<>();
        for(int i = 0; i < Math.ceil((double)n/m); ++i){//天花板保证车位足够
            List<Integer> temp = new ArrayList<>();
            for(int j = 0; j < m; ++j){
                if(sc.hasNext()) temp.add(sc.nextInt());
            }
            r.add(temp);
        }
        boolean flag = false;
        for(int i = r.size() - 1; i >= 0; --i){//逆序输出即可
            for(int j = 0; j < r.get(i).size(); ++j){
                if(flag) System.out.print(" ");//第一个不用输出空格
                else flag = true;
                System.out.print(r.get(i).get(j));
            }
        }
    }
}
全部评论

相关推荐

3 收藏 评论
分享
牛客网
牛客企业服务