时间限制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
* @优快云 https://blog.youkuaiyun.com/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();
}
}
}