首页 > 试题广场 >

实现方法:print_rotate_matrix(int[]

[问答题]
实现方法:print_rotate_matrix(int[] matrix, int n), 将一个n×n二维数组逆时针旋转45度后打印,例如,下图显示一个3×3的二维数组及其旋转后屏幕输出的效果。

描述思路(2分)

请用你熟悉的语言编码显示(8分)


	public static void main(String[] args) {
		int[][] arr = new int[][] { { 1, 2, 3, 4 }, { 5, 6, 7, 8 },
			{ 9, 10, 11, 12 }, { 13, 14, 15, 16 } };
		int n = 4;
		for(int i=0; i<n; i++){
			for(int j = 0; j<=i; j++){
				System.out.print(arr[j][j+n-i-1] + " ");
			}
			System.out.println(" ");
		}
		for(int i=n-2; i>=0; i--){
			for(int j = 0; j<=i; j++){
				System.out.print(arr[j+n-i-1][j] + " ");
			}
			System.out.println(" ");
		}
		
	}

发表于 2017-03-23 19:59:53 回复(0)
public class PrintRotateMatrix {
	public static void main(String[] args) {

		int[][] arr = new int[][] { { 1, 2, 3, 4 }, { 5, 6, 7, 8 },
				{ 9, 10, 11, 12 }, { 13, 14, 15, 16 } };

		/**
		 * 打印上半截
		 */
		for (int i = 0, k = arr.length - 1; i < arr.length; i++, k--) {
			for (int j = 0; j < i + 1; j++) {
				System.out.print(arr[j][j + k] + " ");
			}
			System.out.println();
		}

		/**
		 * 打印下半截
		 */
		for (int i = 0, k = 1; i < arr.length - 1; i++, k++) {
			for (int j = i + 1; j < arr.length; j++) {
				System.out.print(arr[j][j - k] + " ");
			}
			System.out.println();
		}

	}
}

发表于 2015-07-30 11:49:25 回复(0)
int main()
{
   int n =3;
   int a[3][3] ={{1,2,3},{4,5,6},{7,8,9}};
   for (int i = 0; i < n; ++i)
   {
     for (int j = 0; j < i+1; ++j)
     {
       std::cout<<a[j][n-i-1+j]<<" ";
     }
     printf("\n");
   }
   for (int i = 0; i < n-1; ++i)
   {
     for (int j = 0; j < n-i-1; ++j)
     {
      std::cout<<a[i+1+j][j]<<" ";
     }
     printf("\n");
   }
   printf("\n");
 
}
发表于 2015-06-11 12:16:22 回复(0)