【嵙大OJ】Problem 1053 ~ 1055:Matrix Problem

矩阵的转置、加法与乘法

Sample Input

1
3 3
1 2 3
4 5 6
7 8 9

Sample Output

1 4 7
2 5 8
3 6 9
#include 
#include 
#include 

int main()
{
    int A[100][100], N, m, n, temp;
    int i, j, I;
    scanf("%d", &N);
    for(I = 1; I <= N; I++)
    {
        scanf("%d%d", &m, &n);
        for(i = 0; i < m; i++)
            for(j = 0; j < n; j++)
                scanf("%d", &A[i][j]);
        // 转置
        /*
        for(i = 0; i < m; i++)
            for(j = 0; j < i; j++)
            {
                temp = A[i][j];
                A[i][j] = A[j][i];
                A[j][i] = temp;
            }
        */// 这样肯定不行, 矩阵不一定是方阵
        if(m >= n)
        {
            for(i = 0; i < m; i++)
            {
                for(j = 0; j <= i; j++)
                {
                    temp = A[i][j];
                    A[i][j] = A[j][i];
                    A[j][i] = temp;
                }
            }
        }
        else
        {
            for(i = 0; i < m; i++)
            {
                for(j = i; j < n; j++)
                {
                    temp = A[i][j];
                    A[i][j] = A[j][i];
                    A[j][i] = temp;
                }
            }
        }

        for(i = 0; i < n; i++)  // m 和 n 要对调,因为行和列转置过来了
        {
            for(j = 0; j < m; j++)
            {
                printf("%d", A[i][j]);
                if(j != m - 1)
                    printf(" ");
            }
            printf("\n");
        }printf("\n");

    }
    return 0;
}

Sample Input

3 3
1 2 3
4 5 6
7 8 9
3 3
9 8 7
6 5 4
3 2 1
3 3
1 1 1
1 1 1
1 1 1
2 2
2 2
2 2
1 1
0
0 0

Sample Output

10 10 10
10 10 10
10 10 10

Not satisfied the definition of matrix addition!
#include 
#include 

void obtain_matrix(int m, int n, int t[][105])  // 不太会二元数组和函数的组合,可能是这里总是wrong answer 50%
{
    int i, j;
    for(i = 0; i < m; i++)
        for(j = 0; j < n; j++)
            scanf("%d", &t[i][j]);
}

int main()
{
    int A[105][105], B[105][105], ma, na, mb, nb;
    int i, j;
    while(scanf("%d%d", &ma, &na) != EOF && (ma != 0 && na != 0))
    {

        obtain_matrix(ma, na, A);
        scanf("%d%d", &mb, &nb);

        if(mb == 0 && nb == 0) break;
        else
        {
            obtain_matrix(mb, nb, B);
            if(ma == mb && na == nb)
            {
                for(i = 0; i < ma; i++)
                {
                    for(j = 0; j < na; j++)
                    {
                        printf("%d", A[i][j] + B[i][j]);
                        if(j != ma - 1)
                            printf(" ");
                    }printf("\n");
                }printf("\n");
            }
            else printf("Not satisfied the definition of matrix addition!\n\n");
        }
    }
    return 0;
}




你可能感兴趣的:(【嵙大OJ】Problem 1053 ~ 1055:Matrix Problem)