用C#编写一个,任意一个3行3列的数字,按照斜对角线交换位置的程序。。。。

例如原来矩阵是这样的
1 2 3
4 5 6
7 8 9
最后结果是
1 4 7
2 5 8
3 6 9

int[,] arrayA = new int[3, 3]{
{1,2,3},
{4,5,6},
{7,8,9}
};

int[,] arrayB = new int[3, 3]{
{1,2,3},
{4,5,6},
{7,8,9}
};

for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
Console.Write(arrayA[i,j]+" ");
}
Console.WriteLine();
}

for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
arrayA[i, j] = arrayB[j, i];
//就是把横纵的下标换个位置
}
}

Console.WriteLine();

for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
Console.Write(arrayA[i, j] + " ");
}
Console.WriteLine();
}

Console.ReadKey();
温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-05-22
int temp=0;
for(int i=0;i<m;i++)
for(int j=0;j<m;j++)
{ if(i!=j)
{ temp=a[j][i];
a[j][i]=a[i][j];
a[i][j]=temp;
}
}追问

谢谢

第2个回答  2012-05-22
方法有很多,我用二维数组弄了个,不知道你用不用得上~~
int[,] nums = new int[3, 3] {
{1,2,3},
{4,5,6},
{7,8,9}
};
for (int i = 0; i < nums.Length/nums.GetLength(0); i++)
{
for (int j = 0; j < nums.GetLength(0); j++)
{
Console.Write(nums[i,j]);
}
Console.WriteLine();
}
Console.WriteLine("*********************");
for (int i = 0; i < nums.Length / nums.GetLength(0); i++)
{
for (int j = 0; j < nums.GetLength(0); j++)
{
Console.Write(nums[j, i]);
}
Console.WriteLine();
}追问

谢谢你,挺好的。

追答

不用客气~

第3个回答  2012-05-22
用两个数组,注意下标,如原数组中数字2的数组表示为a[0,1];转换后数字2的数组表示为b[1,0];
用两重循环遍历解决,思路就是这样!追问

谢谢啊。