Print sawtooth matrix (C++)

A sawtooth matrix refers to a matrix with a different number of elements in each row, such as:
3 5 2 6 1
2 3 4
1 6 2 7
Read in several pairs of integers (x, y), which means adding at the end of the xth row One element y. Output the final sawtooth array. Initially the matrix is ​​empty.
Input format
Enter two integers n, m (1≤n,m≤10000) in the first line, where n represents the number of rows in the sawtooth array, and m represents the total number of inserted elements.
Next, there are a total of m rows, each with two integers x, y (1≤x≤n, 0≤y≤10000), which means that an element y is inserted at the end of the xth row.
Output format A
total of n lines are output, and each line is a number of integers separated by spaces. If there are no elements in a row, a blank row is output.

Sample input
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

Sample output
3 5 2 6 1
2 3 4
1 6 2 7

#include<iostream>
#include<vector>
using namespace std;
vector<int> mat[10005];
int main()
{
    
    
	int n ,m ,x ,y;
	cin>>n>>m;
	for(int i=0;i<m;i++){
    
    
		cin>>x>>y;
		mat[x].push_back(y);
	}
	for(int i=1;i<=n;i++){
    
    
		for(int j=0;j<mat[i].size();j++){
    
    
			if(j!=mat[i].size()-1){
    
    
				cout<<mat[i][j]<<' ';
			}else{
    
    
				cout<<mat[i][j]<<endl;
			}
		}
		cout<<endl;
	}
	return 0;	
} 

Guess you like

Origin blog.csdn.net/weixin_51430516/article/details/115353764