UVA10895 Matrix Transpose【数据结构】

A matrix is a rectangular array of elements, most commonly numbers. A matrix with m rows and n columns is said to be an m-by-n matrix. For example,

is a 4-by-3 matrix of integers.
    The individual elements of a matrix are usually given lowercase symbols and are distinguished by subscripts. The ith row and jth column of matrix A is usually referred to as aij . For example, a23 = −1. Matrix subscripts are 1-based.
    The transpose of a matrix M, denoted MT, is formed by interchanging the rows and columns of M. That is, the ij-th element of MT is the ji-th element of M. For example, the transpose of matrix A above is:

    A matrix is said to be sparse if there are relatively few non-zero elements. As a m-by-n matrix has mn number of elements, storing all elements of a large sparse matrix may be inefficient as there would be many zeroes. There are a number of ways to represent sparse matrices, but essentially they are all the same: store only the non-zero elements of the matrix along with their row and column.
    You are to write a program to output the transpose of a sparse matrix of integers.
Input
You are given several sparse matrix in a row, each of them described as follows. The first line of the input corresponds to the dimension of the matrix, m and n (which are the number of rows and columns, respectively, of the matrix). You are then given m sets of numbers, which represent the rows of the matrix. Each set consists of two lines which represents a row of the matrix. The first line of a set starts with the number r, which is the number of non-zero elements in that row, followed by r numbers which correspond to the column indices of the non-zero elements in that row, in ascending order; the second line has r integers which are the matrix elements of that row. For example, matrix A above would have the following representation:
4 3
3 1 2 3
1 3 2
2 2 3
4 -1
0

3 1 2 3
5 -2 11
    Note that for a row with all zero elements, the corresponding set would just be one number, ‘0’, in the first line, followed by a blank line.
    You may assume:
• the dimension of the sparse matrix would not exceed 10000-by-10000,
• the number of non-zero element would be no more than 1000,
• each element of the matrix would be in the range of -10000 to 10000, and
• each line has no more than 79 characters.
Output
For each input case, the transpose of the given matrix in the same representation.
Sample Input
4 3
3 1 2 3
1 3 2
2 2 3
4 -1
0

3 1 2 3
5 -2 11
Sample Output
3 4
2 1 4
1 5
3 1 2 4
3 4 -2
3 1 2 4
2 -1 11

问题链接UVA10895 Matrix Transpose
问题简述:(略)
问题分析
    输出稀疏矩阵的转置。
    稀疏矩阵一般可以用行列值的结构数组来存储,求转置只需要重新排序一下输出即可。另外一种做法是使用2个向量来实现。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* UVA10895 Matrix Transpose */

#include <bits/stdc++.h>

using namespace std;

const int N = 1000;
struct Node {
    int r, c, v;
} matrix[N];
int a[N];

bool cmp(Node a, Node b)
{
    return a.c == b.c ? a.r < b.r : a.c < b.c;
}

int main()
{
    int m, n, k;
    while(~scanf("%d%d", &m, &n)) {
        int cnt = 0;
        for(int i = 1; i <= m; i++) {
            scanf("%d", &k);
            for(int j = 0; j < k; j++)
                scanf("%d", &a[j]);
            for(int j = 0; j < k; j++) {
                matrix[cnt].r = i;
                matrix[cnt].c = a[j];
                scanf("%d", &matrix[cnt].v);
                cnt++;
            }
        }

        printf("%d %d\n", n, m);
        sort(matrix, matrix + cnt, cmp);
        k = 0;
        for(int i = 1; i <= n; i++) {
            int sz = 0;
            while(matrix[k].c == i) k++, sz++;
            printf("%d", sz);
            for(int j = k - sz; j < k; j++)
                printf(" %d", matrix[j].r);
            printf("\n");
            for(int j = k - sz; j < k; j++) {
                if(j > k - sz) printf(" ");
                printf("%d", matrix[j].v);
            }
            printf("\n");
        }
    }

    return 0;
}

AC的C++语言程序如下:

/* UVA10895 Matrix Transpose */

#include <bits/stdc++.h>

using namespace std;

const int N = 1000;
int a[N + 1];

int main()
{
    int m, n, k, x;
    while(~scanf("%d%d", &m, &n)) {
        vector<int> v1[N + 1], v2[N + 1];
        for(int i = 1; i <= m; i++) {
            scanf("%d", &k);
            for(int j = 1; j <= k; j++)
                scanf("%d", &a[j]);
            for(int j = 1; j <= k; j++) {
                scanf("%d", &x);
                v1[a[j]].push_back(i);
                v2[a[j]].push_back(x);
            }
        }

        printf("%d %d\n", n, m);
        for(int i = 1; i <= n; i++) {
            int sz = v1[i].size();
            printf("%d", sz);
            for(int j = 0; j < sz; j++)
                printf(" %d", v1[i][j]);
            if(sz == 0)
                printf("\n\n");
            else {
                printf("\n%d", v2[i][0]);
                for(int j = 1; j < sz; j++)
                    printf(" %d", v2[i][j]);
                printf("\n");
            }
        }
    }

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值