我是靠谱客的博主 冷艳路灯,最近开发中收集的这篇文章主要介绍长宽相等的矩阵(二维数组)逆时针旋转90度,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

将长宽相等的矩阵(二维数组)逆时针旋转90度,例如,有如下一个二维数组

00 10 20 30 40 50 60  
01 11 21 31 41 51 61  
02 12 22 32 42 52 62  
03 13 23 33 43 53 63  
04 14 24 34 44 54 64  
05 15 25 35 45 55 65  
06 16 26 36 46 56 66  


将其逆时针旋转90度,一种比较快捷的方法是通过两次位置调换即可:

第一步,将矩阵沿着自“左上”到“右下”的对角线对调,可得到如下的对调之后的二维数组:

00 01 02 03 04 05 06  
10 11 12 13 14 15 16  
20 21 22 23 24 25 26  
30 31 32 33 34 35 36  
40 41 42 43 44 45 46  
50 51 52 53 54 55 56  
60 61 62 63 64 65 66  


第二步,将矩阵上下行对调,即第一行与倒数第一行对调,第二行与倒数第二行对调,依次类推,可得到如下最终版的二维数组(也即原始二维数组逆时针旋转90度的数组):

60 61 62 63 64 65 66  
50 51 52 53 54 55 56  
40 41 42 43 44 45 46  
30 31 32 33 34 35 36  
20 21 22 23 24 25 26  
10 11 12 13 14 15 16  
00 01 02 03 04 05 06  


相关的java版小测试程序为:

public class Test {
	public static void main(String[] args) {
		String m_TestData[][] = {
				{"00","10","20","30","40","50","60"},
				{"01","11","21","31","41","51","61"},
				{"02","12","22","32","42","52","62"},
				{"03","13","23","33","43","53","63"},
				{"04","14","24","34","44","54","64"},
				{"05","15","25","35","45","55","65"},
				{"06","16","26","36","46","56","66"}
		};
		int iX, iY;
		String tmpData;
		int m_iHeight = 7;
		int m_iWidth = 7;
		System.out.println("原始数据:");
		for(iY=0;iY<m_iHeight; ++iY){
			for(iX=0; iX<m_iWidth; ++iX)
				System.out.print(m_TestData[iY][iX] + " ");
			System.out.println(" ");
		}
		for(iY=0; iY<m_iHeight; ++iY){
			iX= iY;
			for(; iX<m_iWidth; ++iX){
				tmpData = m_TestData[iY][iX];
				m_TestData[iY][iX] = m_TestData[iX][iY];
				m_TestData[iX][iY] = tmpData;
			}
		}	
		System.out.println("对角调换之后的数据:");
		for(iY=0;iY<m_iHeight; ++iY){
			for(iX=0; iX<m_iWidth; ++iX)
				System.out.print(m_TestData[iY][iX] + " ");
			System.out.println(" ");
		}
		for(iY=0; iY<m_iHeight/2; ++iY){
			for(iX=0; iX<m_iWidth; ++iX){
				tmpData = m_TestData[iY][iX];
				m_TestData[iY][iX] = m_TestData[m_iHeight-iY-1][iX];
				m_TestData[m_iHeight-iY-1][iX] = tmpData;
			}	
		}
		System.out.println("上下对调之后的数据:");
		for(iY=0;iY<m_iHeight; ++iY){
			for(iX=0; iX<m_iWidth; ++iX)
				System.out.print(m_TestData[iY][iX] + " ");
			System.out.println(" ");
		}
	}
}


 

最后

以上就是冷艳路灯为你收集整理的长宽相等的矩阵(二维数组)逆时针旋转90度的全部内容,希望文章能够帮你解决长宽相等的矩阵(二维数组)逆时针旋转90度所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(41)

评论列表共有 0 条评论

立即
投稿
返回
顶部