当年的面试题:矩阵转置
时间:2010-06-12 来源:sinodragon21
#include <stdio.h>
#define M 2
#define N 5
//int a[M][N] = { {1, 2, 3, 4, 5} , {6, 7, 8, 9, 10} , {11, 12, 13, 14, 15}};
int a[M][N] = { {1, 2, 3, 4, 5} , {6, 7, 8, 9, 10}};
int main()
{
int i = 0;
int j = 0;
int temp = 0;
for(i=0; i<M; i++)
{
for(j=0; j<N; j++)
{
printf("%d\t", a[i][j]);
}
printf("\n");
}
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;
}
}
printf("\nAfter Reverse, The Matrix is:\n");
for(i=0; i<N; i++)
{
for(j=0; j<M; j++)
{
printf("%d\t", a[i][j]);
}
printf("\n");
}
}
#define M 2
#define N 5
//int a[M][N] = { {1, 2, 3, 4, 5} , {6, 7, 8, 9, 10} , {11, 12, 13, 14, 15}};
int a[M][N] = { {1, 2, 3, 4, 5} , {6, 7, 8, 9, 10}};
int main()
{
int i = 0;
int j = 0;
int temp = 0;
for(i=0; i<M; i++)
{
for(j=0; j<N; j++)
{
printf("%d\t", a[i][j]);
}
printf("\n");
}
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;
}
}
printf("\nAfter Reverse, The Matrix is:\n");
for(i=0; i<N; i++)
{
for(j=0; j<M; j++)
{
printf("%d\t", a[i][j]);
}
printf("\n");
}
}
相关阅读 更多 +