本文介绍了如何旋转二次阵列的指定行和列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,我是新来的Java,也堆放overflow.I已被要求创建方法应该旋转二次数组的指定行和旋转阵列的指定列。我想知道我怎么会去这是我的Java知识是基本相当。谢谢你。
包分配1;
公共类益智{ 公共静态最终诠释N = 4;
公共静态最终诠释NUMBER_OF_ROTATIONS = 5; 公共静态无效的主要(字串[] args){
INT [] [] =砌新INT [N] [N];
复位(拼图);
测试(拼图);
复位(拼图);
争夺(拼图);
的System.out.println(###测试益智游戏\\ n);
玩(拼图);
} 公共静态无效打印(INT [] []拼图){
的for(int []行:拼图){
对于(INT ELEM:行){
System.out.printf(%4D,ELEM);
}
的System.out.println();
}
的System.out.println();
} 公共静态无效测试(INT [] []拼图){
的System.out.println(###测试方法重置\\ n);
打印(拼图);
的System.out.println(###测试方法旋转的\\ n);
打印(拼图);
的for(int i = 0; I< N;我++){
的System.out.println(### rotateColumn(+ I +)\\ n);
rotateColumn(拼图,我);
打印(拼图);
的System.out.println(### rotateRow(+ I +)\\ n);
rotateRow(拼图,我);
打印(拼图);
}
复位(拼图);
的System.out.println(###测试随机轮换的\\ n);
打印(拼图);
的for(int i = 0;我小于5;我++){
randomRotation(拼图);
打印(拼图);
}
} 公共静态无效复位(INT [] []拼图){
的for(int i = 0; I< N;我++){
对于(INT J = 0; J< N; J ++)
拼图[I] [J] = I * N + J;
}
} 公共静态无效的争夺(INT [] []拼图){
的for(int i = 0; I< NUMBER_OF_ROTATIONS;我++){
randomRotation(拼图);
}
}
公共静态无效rotateRow(INT [] []编曲,诠释行){
} 公共静态无效rotateColumn(INT [] []编曲,诠释列){
} 公共静态无效randomRotation(INT [] []拼图){
} 静态无效播放(INT [] []拼图){
}}
解决方案
旋转具体行:
公共静态无效rotateRow(INT [] []编曲,诠释行){
INT [] R = {ARR [行] [0],ARR [行] [1],编曲[行] [2],编曲[行] [3]};
为(中间体J = r.length - 1; J&大于0; j--){
INT温度= R [J]。
- [R [J] = R [J - 1];
- [R [J - 1] =温度;
}
改编[行] [0] = R [0];
改编[行] [1] = R [1];
改编[行] [2] = R [2];
改编[行] [3] = R [3];
}
Rortating特定列:
公共静态无效rotateColumn(INT [] []编曲,诠释列){
INT [] R = {改编[0] [列],ARR [1] [列],ARR [2] [列],ARR [3] [列]};
为(中间体J = r.length - 1; J&大于0; j--){
INT温度= R [J]。
- [R [J] = R [J - 1];
- [R [J - 1] =温度;
}
改编[0] [列] = R [0];
改编[1] [列] = R [1];
ARR [2] [列] = R [2];
改编[3] [列] = R [3];
}
旋转随机:
公共静态无效randomRotation(INT [] []拼图){
随机兰特=新的随机();
INT N = rand.nextInt(3);
rotateRow(拼图,3);
}
Hello i am new to java and also stack overflow.I have been asked to create method should rotate the specified row of a quadratic array and rotate the specified column of the array. I was wondering how i would go about this as my java knowledge is quite basic. Thank you.
package Assignment1;
public class puzzle {
public static final int N = 4;
public static final int NUMBER_OF_ROTATIONS = 5;
public static void main(String[] args) {
int[][] puzzle = new int[N][N];
reset(puzzle);
test(puzzle);
reset(puzzle);
scramble(puzzle);
System.out.println("### Testing puzzle game play\n");
play(puzzle);
}
public static void print(int[][] puzzle) {
for (int[] row : puzzle) {
for (int elem : row) {
System.out.printf("%4d", elem);
}
System.out.println();
}
System.out.println();
}
public static void test(int[][] puzzle) {
System.out.println("### Testing reset method\n");
print(puzzle);
System.out.println("### Testing rotate methods\n");
print(puzzle);
for (int i = 0; i < N; i++) {
System.out.println("### rotateColumn(" + i + ")\n");
rotateColumn(puzzle, i);
print(puzzle);
System.out.println("### rotateRow(" + i + ")\n");
rotateRow(puzzle, i);
print(puzzle);
}
reset(puzzle);
System.out.println("### Testing random rotations\n");
print(puzzle);
for (int i = 0; i < 5; i++){
randomRotation(puzzle);
print(puzzle);
}
}
public static void reset(int[][] puzzle) {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++)
puzzle[i][j] = i * N + j;
}
}
public static void scramble(int[][] puzzle) {
for (int i = 0; i < NUMBER_OF_ROTATIONS; i++) {
randomRotation(puzzle);
}
}
public static void rotateRow(int[][] arr, int row) {
}
public static void rotateColumn(int[][] arr, int column) {
}
public static void randomRotation(int[][] puzzle) {
}
static void play(int[][] puzzle) {
}
}
解决方案
Rotating Specific Row:
public static void rotateRow(int[][] arr, int row) {
int[] r = {arr[row][0], arr[row][1], arr[row][2], arr[row][3]};
for (int j = r.length - 1; j > 0; j--) {
int temp = r[j];
r[j] = r[j - 1];
r[j - 1] = temp;
}
arr[row][0] = r[0];
arr[row][1] = r[1];
arr[row][2] = r[2];
arr[row][3] = r[3];
}
Rortating Specific Column:
public static void rotateColumn(int[][] arr, int column) {
int[] r = {arr[0][column], arr[1][column], arr[2][column], arr[3][column]};
for (int j = r.length - 1; j > 0; j--) {
int temp = r[j];
r[j] = r[j - 1];
r[j - 1] = temp;
}
arr[0][column] = r[0];
arr[1][column] = r[1];
arr[2][column] = r[2];
arr[3][column] = r[3];
}
Rotating Randomly:
public static void randomRotation(int[][] puzzle) {
Random rand = new Random();
int n = rand.nextInt(3);
rotateRow(puzzle, 3);
}
这篇关于如何旋转二次阵列的指定行和列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!