数据结构实验之数组一:矩阵转置

来源:互联网 发布:cmge中手游 知乎 编辑:程序博客网 时间:2024/06/06 02:04

Problem Description

数组——矩阵的转置
给定一个m*n的矩阵(m,n<=100),求该矩阵的转置矩阵并输出。

Input

输入包含多组测试数据,每组测试数据格式如下:
第一行包含两个数m,n
以下m行,每行n个数,分别代表矩阵内的元素。
(保证矩阵内的数字在int范围之内)

Output

对于每组输出,输出给定矩阵的转置矩阵。两组输出之间用空行隔开。

Example Input

2 3
1 2 3
4 5 6
1 1
1
Example Output

1 4
2 5
3 6

1

数组的动态分配的方法不会!

#include <iostream>using namespace std;#include <stdio.h>#include <stdlib.h>int a[150][150],b[150][150];int main(){    int m,n;    while(~scanf("%d %d",&m,&n))    {        int i,j;        for(i=0;i<m;i++)//二维数组的输入        {            for(j=0;j<n;j++)            {                scanf("%d",&a[i][j]);                b[j][i]=a[i][j];//转置            }        }        for(i=0;i<n;i++)//m行n列 变为 n行m列        {            for(j=0;j<m-1;j++)            {                printf("%d ",b[i][j]);            }            printf("%d\n",b[i][m-1]);        }        printf("\n");    }    return 0;}
阅读全文
0 0
原创粉丝点击