简体   繁体   English

矩阵未向右旋转 90 度

[英]Matrix not rotating 90 degree to Right

I want to rotate matrix 90 degree to right, using a single dimension array but it does not give the desired output我想使用一维数组将矩阵向右旋转 90 度,但它没有给出所需的 output

I used a simple method before and it did work, so for practice i tried to do it with the help of a single dimension array.我之前使用了一个简单的方法,它确实有效,所以为了练习,我尝试在单维数组的帮助下进行。

import java.util.Scanner;
public class nine
{
    void main()
    {
        int M, i ,j;
        int z=0;
        int arr[][], x[];

        Scanner sc=new Scanner(System.in);
        System.out.println("ENTER THE NUMBER OF ROW AND COLUMN OF MATRIX");
        M=sc.nextInt();

        arr=new int[M][M];
        x=new int[M*M];
        System.out.println("ENTER THE ELEMENTS IN MATRIX ");
        for(i=0;i< M;i++)
        {
            for(j=0;j< M;j++)
            {
                arr[i][j]=sc.nextInt();
            }
        }
        System.out.println( );
        System.out.println( "------------------------------------------------------");
        System.out.println("ORIGINAL MATRIX \n");
        for(i=0;i< M;i++)
        {
            for(j=0;j< M;j++)
            {
                System.out.print(arr[i][j]+" ");
            }
            System.out.println();
        }
        System.out.println( );
        System.out.println( "------------------------------------------------------");
        
        /* giving matrix x the value of arr from bottom. 
         * example matrix-
            1 2 3 
            4 5 6 
            7 8 9 
            value stored in x- {7 8 9 4 5 6 3 2 1}
        */
        for(i=M-1;i>0;i--)
        {
            for(j=0;j< M;j++)
            {
                x[z]=arr[i][j];
                z++;
            }
        }
        z=0; 
        /* 
         * Adding values of matrix x in matrix arr veritcally. 
         * 
           */
        for(j=0;j< M;j++)
        {
            for(i=0;i< M;i++)
            {
                arr[i][j]= x[z];
                z++;
            }
        }
         /* 
         * Desired output from the example input- 
         *      7 4 1 
                8 5 2 
                9 6 3 
         * 
           */
        System.out.println("ROTATED MATRIX \n");
        for(i=0;i< M;i++)
        {
            for(j=0;j< M;j++)
            {
                System.out.print(arr[i][j]+" " );

            }
            System.out.println( );
        }
    }
}


The original array is double dimension and gives value to the single dimension array in the order stated. 原始数组是二维的,并按规定的顺序为一维数组赋值。

The output I want is:我想要的 output 是:

图像1

The output I am getting is is:我得到的 output 是:

img2

To rotate, you need something like this:要旋转,你需要这样的东西:

int arr[][];
int dest[][];

arr = new int[M][M];
dest = new int[M][M];

for (int i=0; i<M, i++) {
  for (int j=0; j<M, j++) {
    dest[M-j-1][i] = arr[i][j];
  }
}

also, you count down loops need to include zero此外,您倒计时循环需要包括零

for(i=M-1;i>0;i--)

should be应该

for(i=M-1;i>=0;i--)

( turning the comment by Ricky Mo into an answer with minimal changes and explanation ) 将 Ricky Mo 的评论变成一个改动和解释最少的答案

You are very close.你很亲密。 No need to fundamentally change your approach.无需从根本上改变您的方法。
However, the output you get shows clearly that your code does not write to the last column.但是,您得到的 output 清楚地表明您的代码没有写入最后一列。
An obvious conclusion is that your loop setups are not correct.一个明显的结论是您的循环设置不正确。
And indeed (as Ricky Mo put succinctly):确实(正如 Ricky Mo 简洁地说的那样):

change for(i=M-1;i>0;i--) to for(i=M-1;i>=0;i--)for(i=M-1;i>0;i--)更改for(i=M-1;i>=0;i--)

Obviously it iterates once more often.显然它会更频繁地迭代。
And that ends up in the right (ie the 0 values column) because of the indexing magic, which you correctly implemented.由于您正确实现了索引魔法,这最终出现在右侧(即 0 值列)。

And the output is: output 是:

ENTER THE NUMBER OF ROW AND COLUMN OF MATRIX
ENTER THE ELEMENTS IN MATRIX 

------------------------------------------------------
ORIGINAL MATRIX 

1 2 3 
4 5 6 
7 8 9 

------------------------------------------------------
ROTATED MATRIX 

7 4 1 
8 5 2 
9 6 3 

eg here:例如这里:
https://www.tutorialspoint.com/compile_java_online.php https://www.tutorialspoint.com/compile_java_online.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM