zoukankan      html  css  js  c++  java
  • 打印锯齿矩阵

    锯齿矩阵是指每一行包含的元素个数不相同的矩阵,比如:
    1
    3 5 2 6 1
    2
    2 3 4
    3
    1 6 2 7
    读入若干对整数 (x,y),表示在第 xx 行的末尾加上一个元素 y。输出最终的锯齿数组。初始时矩阵为空。

    输入格式

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

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

    输出格式

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

    样例输入

    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

    • 完整代码
    //vector<T> vec;  
    //构造一个名为vec的储存数据类型
    //为T的动态数组。其中T为需要储存的数据类型
    //初始时vec为空
    //push_back  末尾添加一个元素
    //pop_back   在末尾弹出一个元素
    //size       获取长度
    //clear       清空
    
    //修改vector其中的某个元素,直接赋值,比如vec[1]=3;
    /*vector的方法size()可以直接获取长度,通过[]可以直接获取其中的元素,和数组相同*/
    //clear()会清空vector中内容,但是不会重新分配
    /*
    如果需要清空vector的内存,一种典型的方法是使用交换,
    即使用一个空的vector和原来的vector进行交换,完成内存的释放
    vector<int>v;
    {
    vector<int> x;
    v.swap(x);
    }
    */
    /*
    Clear content
    Removes all elements from the vector (which are destroyed), leaving the container with a size of 0.
    
    A reallocation is not guaranteed to happen, and the vector capacity is not guaranteed to change due to calling this function. A typical alternative that forces a reallocation is to use swap:
    
    vector<T>().swap(x);   // clear x reallocating
    
    */
    
    #include<iostream>
    #include<vector>
    #include<cstdio>
    #include<cstdlib>
    #include<string>
    #include<algorithm>
    using namespace std;
    int main(void)
    {
        int m, n, i, j, k;
        scanf("%d%d", &n, &m);
        vector<int> ivec[10000];
        for (int k = 0; k < m; k++)
        {
            scanf("%d%d", &i, &j);
            ivec[i - 1].push_back(j);
        } 
        for (i = 0; i < n; i++)
        {
            if (!ivec[i].size())
            {
                printf("
    ");
                continue;
            }
            for (j = 0; j < ivec[i].size()-1; j++)
            {
                printf("%d ", ivec[i][j]);
            }
            printf("%d", ivec[i][j]);
            printf("
    ");
        }
        return 0;
    }
  • 相关阅读:
    高中教材记录
    帮我解决逆序问题的网友:@18数院啦啦啦。恶人:16 师大 很菜 刘琳(2196879114) 2:32:49 PM
    丘维声的视频地址,全部课堂
    理工医疗报销电话
    可汗化学和二项式定理的地址
    二次函数问题
    *0000181894BD*---------北大医院条形码
    三月里的小雨
    语文容易读错的字
    mongodb部署
  • 原文地址:https://www.cnblogs.com/FlyerBird/p/9052557.html
Copyright © 2011-2022 走看看