计蒜客 集合 打印锯齿矩阵

 时间限制1000ms   空间限制131072K

锯齿矩阵是指每一行包含的元素个数不相同的矩阵,比如:

1
3 5 2 6 1
2
2 3 4
3
1 6 2 7

读入若干对整数 (x,y)(x,y),表示在第 xx 行的末尾加上一个元素 yy。输出最终的锯齿数组。初始时矩阵为空。

输入格式
第一行输入两个整数 n,m(1 \leq n,m \leq 10000)n,m(1≤n,m≤10000),其中 nn 表示锯齿数组的行数,mm 表示插入的元素总数。

接下来一共 mm 行,每行两个整数 x,y(1 \leq x \leq n, 0 \leq y \leq 10000)x,y(1≤x≤n,0≤y≤10000),表示在第 xx 行的末尾插入一个元素 yy。

输出格式
一共输出 nn 行,每行若干个用空格分隔的整数。如果某行没有任何元素,则输出一个空行。

样例输入

3 12
1 3
2 2
2 3
2 4
3 1
3 6
1 5
1 2
1 6
3 2
3 7
1 1

样例输出

3 5 2 6 1
2 3 4
1 6 2 7
package www.jisuanke.ds;


import java.util.ArrayList;
import java.util.Scanner;

/**
 * @author wangchong
 * @date 2019/4/30 16:09
 * @email 876459397@qq.com
 * @CSDN https://blog.csdn.net/wfcn_zyq
 * @describe
 */
public class Code_01_SpecialMatrix {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        int m = input.nextInt();
        ArrayList[] arrayList = new ArrayList[n];
        for (int i = 0; i < n; i++) {
            arrayList[i] = new ArrayList<Integer>();
        }
        for (int i = 0; i < m; i++) {
            int x = input.nextInt();
            int y = input.nextInt();
            arrayList[x - 1].add(y);
        }
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < arrayList[i].size(); j++) {
                if (j < arrayList[i].size() - 1) {
                    System.out.print(arrayList[i].get(j) + " ");
                } else {
                    System.out.print(arrayList[i].get(j));
                }
            }
            System.out.println();
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值