gpt4 book ai didi

java - 将方阵旋转 90 度

转载 作者:行者123 更新时间:2023-12-01 10:30:26 24 4
gpt4 key购买 nike

这是我在 Java 中将方阵顺时针旋转 90 度的代码。

import java.util.ArrayList;

public class Main {

public static void main(String[] args) {
// write your code here
int size = 4;
int[][] matrix = new int[size][size];
for (int i = 0; i < size ; i++) {
for (int j = 0; j < size; j++) {

matrix[i][j] = (i+(2*j));
}
}
trace(matrix, size);
rotate(matrix);
System.out.println("________________________");
trace(matrix, size);

}

public static void rotate(int[][] matrix){
int stop = matrix.length -1;

rotate(matrix, stop, 0);
}

public static void rotate(int[][] matrix, int stop, int start){

if(start - stop == 1) return;

ArrayList<Integer> temp = new ArrayList<>();

//save top row in an array
for (int i = start; i <= stop ; i++) {
temp.add(matrix[start][i]);
}

//left goes to top
for (int i = start; i <= stop ; i++) {
matrix[start][i] = matrix[stop - i][start];
}

//bottom goes to left
for (int i = start; i <= stop ; i++) {
matrix[i][start] = matrix[stop][i];
}

//right goes to bottom
for (int i = start; i <= stop ; i++) {
matrix[stop][stop - i] = matrix[i][stop];
}

//temp array goes to right
for (int i = 0; i < temp.size() ; i++) {
matrix[start + i][stop] = temp.get(i);
}

rotate(matrix, stop-1, start + 1);
}



public static void trace(int[][] matrix, int size){
for (int i = 0; i < size; i++) {
for (int j = 0; j < size ; j++) {
System.out.print(matrix[i][j] + " ");

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

原始数组和输出如下所示,

0 2 4 6 
1 3 5 7
2 4 6 8
3 5 7 9
________________________
3 2 1 0
5 4 3 2
6 2 5 4
3 8 7 6

显然答案并不完全正确。虽然已经快到了。我对这些错误有点迷失,感谢任何帮助。

最佳答案

这段代码看起来工作正常。这里的测试用例是一个 8x8 矩阵,针对您的测试用例将其缩放回 4x4。

public class Main {

public static void main(String[] args) {
// write your code here
int size = 8;
int[][] matrix = new int[size][size];
for (int i = 0; i < size ; i++) {
for (int j = 0; j < size; j++) {

matrix[i][j] = (i+(2*j));
}
}
trace(matrix, size);
rotate(matrix);
System.out.println("________________________");
trace(matrix, size);

}

public static void rotate(int[][] matrix){
int stop = matrix.length -1;

rotate(matrix, stop, 0);
}

public static void rotate(int[][] matrix, int stop, int start){

if(start - stop == 1) return;

int [] temp = new int[stop-start];

//save top row in an array
for (int i = 0; i < stop-start ; i++) {
temp[i] = matrix[start][start+i+1];
}
//System.out.println(Arrays.toString(temp));

//left goes to top
for (int i = start; i < stop ; i++) {
matrix[start][start+stop-i] = matrix[i][start];
}

//bottom goes to left
for (int i = start; i < stop ; i++) {
matrix[i][start] = matrix[stop][i];
}

//right goes to bottom
for (int i = start; i < stop ; i++) {
matrix[stop][i] = matrix[start+stop-i][stop];
}

//temp array goes to right
for (int i = 0; i < temp.length ; i++) {
matrix[start + i + 1][stop] = temp[i];
}

rotate(matrix, stop-1, start + 1);
}



public static void trace(int[][] matrix, int size){
for (int i = 0; i < size; i++) {
for (int j = 0; j < size ; j++) {
System.out.printf("%2d ", matrix[i][j]);

}
System.out.println();
}
}

}

关于java - 将方阵旋转 90 度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35120734/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com