使用数组打印蛇形图案

huangapple go评论104阅读模式
英文:

Printing a snake pattern using an array

问题

以下是翻译好的部分:

  1. //蛇形移动带有数字
  2. import java.util.Scanner;
  3. public class SnakeMove {
  4. public static void main(String[] args) {
  5. //创建 Scanner 对象
  6. Scanner inScan = new Scanner(System.in);
  7. //提示用户从 0 到 16 选择行数
  8. System.out.println("选择行数,范围从 0 到 16。");
  9. //使用 nextInt() 方法从用户获取输入
  10. //使用变量 int row
  11. int row = inScan.nextInt();
  12. //提示用户从 0 到 16 选择列数
  13. System.out.println("选择列数,范围从 0 到 16。");
  14. //使用 nextInt() 方法从用户获取输入
  15. //使用变量 int col
  16. int col = inScan.nextInt();
  17. if (row != col) {
  18. System.out.println("再次运行程序,选择相同的行数和列数。");
  19. System.exit(0);
  20. }
  21. int[][] arr = move(row, col);
  22. for (int i = 0; i < arr.length; i++) {
  23. for (int j = 0; j < arr.length; j++) {
  24. System.out.print(arr[i][j] + " ");
  25. }
  26. System.out.println();
  27. }
  28. }//main 方法
  29. static int[][] move(int row, int col) {
  30. boolean flag = true;
  31. int count = 1;
  32. int[][] array = new int[row][col];
  33. for (int j = 0; j < array[0].length; j++) {
  34. if (flag) {
  35. for (int i = 0; i < array.length; i++) {
  36. //将 count 的增量值赋值给特定的数组单元
  37. array[i][j] = count;
  38. count++;
  39. }
  40. flag = false;
  41. } else {
  42. //逐行递减向上移动
  43. for (int i = array.length - 1; i > 0; i--) {
  44. //将 count 的增量值赋值给特定的数组单元
  45. array[i][j] = count;
  46. count++;
  47. }
  48. flag = true;
  49. }
  50. }//逐列增加
  51. return array;
  52. }//move 方法
  53. }//结束 SnakeMove 类
英文:

I'm having trouble with an assignment where we are required to print out this array:

  1. 1 10 11 20 21
  2. 2 9 12 19 22
  3. 3 8 13 18 23
  4. 4 7 14 17 24
  5. 5 6 15 16 25

My code is somewhat correct but it is not printing 10 and 19 where it should be.

My output:

  1. Choose a number for the rows from 0 to 16.
  2. 5
  3. Choose a number for the columns from 0 to 16
  4. 5
  5. 1 0 10 0 19
  6. 2 9 11 18 20
  7. 3 8 12 17 21
  8. 4 7 13 16 22
  9. 5 6 14 15 23

My code:

  1. //snake move with the number
  2. import java.util.Scanner;
  3. public class SnakeMove {
  4. public static void main(String[] args) {
  5. //create Scanner object
  6. Scanner inScan = new Scanner(System.in);
  7. //prompt the user to choose number for the Row from 0 to 16
  8. System.out.println(&quot;Choose a number for the rows from 0 to 16.&quot;);
  9. //take the input from user with nextInt() method
  10. //use the variable int row
  11. int row = inScan.nextInt();
  12. //prompt the user to choose number for the Col from 0 to 16
  13. System.out.println(&quot;Choose a number for the columns from 0 to 16&quot;);
  14. //take the input from user with nextInt()
  15. //use the variable int col
  16. int col = inScan.nextInt();
  17. if (row != col) {
  18. System.out.println(&quot;Run the program again and choose the same number for Row and Col&quot;);
  19. System.exit(0);
  20. }
  21. int[][] arr = move(row, col);
  22. for (int i = 0; i &lt; arr.length; i++) {
  23. for (int j = 0; j &lt; arr.length; j++) {
  24. System.out.print(arr[i][j] + &quot; &quot;);
  25. }
  26. System.out.println();
  27. }
  28. }//main method
  29. static int[][] move(int row, int col) {
  30. boolean flag = true;
  31. int count = 1;
  32. int[][] array = new int[row][col];
  33. for (int j = 0; j &lt; array[0].length; j++) {
  34. if (flag) {
  35. for (int i = 0; i &lt; array.length; i++) {
  36. //assign the increment value of count
  37. // to specific array cells
  38. array[i][j] = count;
  39. count++;
  40. }
  41. flag = false;
  42. } else {
  43. //row decrement going up
  44. for (int i = array.length - 1; i &gt; 0; i--) {
  45. //assign the increment value of count
  46. // to specific array cells
  47. array[i][j] = count;
  48. count++;
  49. }
  50. flag = true;
  51. }
  52. }//column increment
  53. return array;
  54. }//move method
  55. }//end SnakeMove class

Can anyone detect what is causing the error? Any help would be appreciated.

答案1

得分: 1

  1. 我认为这是一个不错的替代方案,容易理解。如果你有同样问题的简化版本,请评论分享。
  2. ## 代码:##
  3. import java.util.Arrays;
  4. import java.util.Scanner;
  5. public class SnakePatternProblem {
  6. public static void main(String[] args) {
  7. Scanner scanner = new Scanner(System.in); // *输入*
  8. int row = scanner.nextInt();
  9. int col = scanner.nextInt();
  10. int[][] array = new int[row][col];
  11. // 用通用表达式初始化数组的第一行
  12. for (int i = 0; i < col; i++) {
  13. if (i % 2 != 0) array[0][i] = col * (i+1);
  14. else array[0][i] = (col * i) + 1;
  15. }
  16. array[0][0] = 1; // 此元素不在上述循环中涵盖。
  17. // 嵌套循环用于根据第一行元素递增和递减。
  18. for (int i = 1; i < row; i++) {
  19. for (int j = 0; j < col; j++) {
  20. if (j % 2 == 0) array[i][j] = array[i-1][j] + 1;
  21. else array[i][j] = array[i-1][j] - 1;
  22. }
  23. }
  24. System.out.println(Arrays.deepToString(array)); // *输出*
  25. }
  26. }
  27. ----------
  28. ## 解释:##
  29. 考虑一个名为 "arr" *5 x 5* 矩阵的第一行,呈蛇形排列:
  30. 1 10 11 20 21
  31. 奇数列中的元素等于 ((当前列 + 1) * 总列数);
  32. 例如:arr[0][1] = (1 + 1) * 5 = 10
  33. ----------
  34. 偶数列中的元素等于 (当前列 * 总列数) + 1
  35. 例如:arra[0][2] = (2 * 5) + 1 = 11;
  36. **重要说明:** 第一行第一列的元素为零
  37. arr[0][0] = 1 -> 必须声明
  38. 现在,剩下的矩阵是从该列的第一个元素开始的递增或递减循环。
  39. 偶数列号的元素在每一行从该列的第一个元素开始递增1
  40. 1 -> 0列的第一个元素
  41. 2 -> (1 + 1)
  42. 3 -> (2 + 1).... 以此类推
  43. 4
  44. 5
  45. ----------
  46. 奇数列号的元素在每一行从该列的第一个元素开始递减1
  47. 10 -> 1列的第一个元素
  48. 9 -> (10 - 1)
  49. 8 -> (9 - 1).... 以此类推
  50. 7
  51. 6
英文:

I believe this is a good alternative and easy to understand. Do comment if you have a simplified version of the same.

Code:

  1. import java.util.Arrays;
  2. import java.util.Scanner;
  3. public class SnakePatternProblem {
  4. public static void main(String[] args) {
  5. Scanner scanner = new Scanner(System.in); // *INPUT*
  6. int row = scanner.nextInt();
  7. int col = scanner.nextInt();
  8. int[][] array = new int[row][col];
  9. // Initializing first row of the array with a generalized expression
  10. for (int i = 0; i &lt; col; i++) {
  11. if (i % 2 != 0) array[0][i] = col * (i+1);
  12. else array[0][i] = (col * i) + 1;
  13. }
  14. array[0][0] = 1; // this element is not covered in the above loop.
  15. // Nested loop for incrementing and decrementing as per the first row of elements.
  16. for (int i= 1; i&lt; row; i++){
  17. for (int j=0; j&lt; col; j++){
  18. if(j%2==0) array[i][j] = array[i-1][j] + 1;
  19. else array[i][j] = array[i-1][j] - 1;
  20. }
  21. }
  22. System.out.println(Arrays.deepToString(array)); // *OUTPUT
  23. }
  24. }

Explanation:

Consider first row of 5 x 5 matrix named "arr" with snake pattern:

  1. 1 10 11 20 21
  2. The element in the odd column is equivalent to ((currentColumn + 1) * Total_No_Of_columns);
  3. Example: arr[0][1] = (1 + 1)* 5 = 10

  1. The element in the even column is equivalent to (currentColumn * Total_No_Of_columns) + 1;
  2. Example: arra[0][2] = (2 * 5) + 1 = 11;

Important Note: element at first row, first column is Zero

  1. arr[0][0] = 1 -&gt; must be declared

Now, the remaining matrix is incrementing or decrementing loop from the first element in that column.

  1. Elements with even column number gets incremented by 1 in each row from the first element of that column.
  2. 1 -&gt; First element of column-0
  3. 2 -&gt; (1 + 1)
  4. 3 -&gt; (2 + 1).... so on
  5. 4
  6. 5

  1. Elements with odd column number gets decremented by 1 in each row from the first element of that column.
  2. 10 -&gt; First element of column - 1
  3. 9 -&gt; (10 - 1)
  4. 8 -&gt; (9 - 1).... so on
  5. 7
  6. 6

答案2

得分: 0

这将生成你所描述的“蛇形”模式。

虽然可以使用三元运算符简化,但我认为这样更易读。

不过,如果有人找到更好的方法,可以在评论中分享一下。

  1. public static int[][] genArray(int length) {
  2. int[][] arr = new int[length][length];
  3. int counter = 0;
  4. for (int col = 0; col < arr.length; col++) {
  5. if (col % 2 == 0) {
  6. for (int row = 0; row < arr.length; row++) {
  7. arr[row][col] = counter++;
  8. }
  9. } else {
  10. for (int row = arr.length - 1; row >= 0; row--) {
  11. System.out.println("row: " + row + ", col: " + col);
  12. arr[row][col] = counter++;
  13. }
  14. }
  15. }
  16. return arr;
  17. }
英文:

This will generate the "snaking" pattern you described.

It could be simplified with ternary but this makes it more readable I think

It would be interesting to find a more clever way about it though, if anyone finds a better way pls comment

  1. public static int[][] genArray(int length) {
  2. int[][] arr = new int[length][length];
  3. int counter = 0;
  4. for (int col = 0; col &lt; arr.length; col++) {
  5. if (col % 2 == 0) {
  6. for (int row = 0; row &lt; arr.length; row++) {
  7. arr[row][col] = counter++;
  8. }
  9. } else {
  10. for (int row = arr.length - 1; row &gt;= 0; row--) {
  11. System.out.println(&quot;row: &quot; + row + &quot;, col: &quot; + col);
  12. arr[row][col] = counter++;
  13. }
  14. }
  15. }
  16. }
  17. return arr;
  18. }

答案3

得分: 0

  1. int m = 5;
  2. int n = 4;
  3. int[][] snakeArr = IntStream.range(0, n)
  4. .mapToObj(i -> IntStream.range(0, m)
  5. // even row - straight order,
  6. // odd row - reverse order
  7. .map(j -> (i % 2 == 0 ? j : m - j - 1) + i * m + 1)
  8. .toArray())
  9. .toArray(int[][]::new);
  10. // output
  11. Arrays.stream(snakeArr)
  12. .map(row -> Arrays.stream(row)
  13. .mapToObj(e -> String.format("%2d", e))
  14. .collect(Collectors.joining(" ")))
  15. .forEach(System.out::println);
  1. 1 2 3 4 5
  2. 10 9 8 7 6
  3. 11 12 13 14 15
  4. 20 19 18 17 16

Transposing snake array:

  1. int[][] transposedSA = new int[m][n];
  2. IntStream.range(0, m).forEach(i ->
  3. IntStream.range(0, n).forEach(j ->
  4. transposedSA[i][j] = snakeArr[j][i]));

// output
Arrays.stream(transposedSA)
.map(row -> Arrays.stream(row)
.mapToObj(e -> String.format("%2d", e))
.collect(Collectors.joining(" ")))
.forEach(System.out::println);

1 10 11 20
2 9 12 19
3 8 13 18
4 7 14 17
5 6 15 16

  1. <details>
  2. <summary>英文:</summary>
  3. You can create such an array as follows:
  4. ```java
  5. int m = 5;
  6. int n = 4;
  7. int[][] snakeArr = IntStream.range(0, n)
  8. .mapToObj(i -&gt; IntStream.range(0, m)
  9. // even row - straight order,
  10. // odd row - reverse order
  11. .map(j -&gt; (i % 2 == 0 ? j : m - j - 1) + i * m + 1)
  12. .toArray())
  13. .toArray(int[][]::new);
  1. // output
  2. Arrays.stream(snakeArr)
  3. .map(row -&gt; Arrays.stream(row)
  4. .mapToObj(e -&gt; String.format(&quot;%2d&quot;, e))
  5. .collect(Collectors.joining(&quot; &quot;)))
  6. .forEach(System.out::println);
  1. 1 2 3 4 5
  2. 10 9 8 7 6
  3. 11 12 13 14 15
  4. 20 19 18 17 16

Transposing snake array:

  1. int[][] transposedSA = new int[m][n];
  2. IntStream.range(0, m).forEach(i -&gt;
  3. IntStream.range(0, n).forEach(j -&gt;
  4. transposedSA[i][j] = snakeArr[j][i]));
  1. // output
  2. Arrays.stream(transposedSA)
  3. .map(row -&gt; Arrays.stream(row)
  4. .mapToObj(e -&gt; String.format(&quot;%2d&quot;, e))
  5. .collect(Collectors.joining(&quot; &quot;)))
  6. .forEach(System.out::println);
  1. 1 10 11 20
  2. 2 9 12 19
  3. 3 8 13 18
  4. 4 7 14 17
  5. 5 6 15 16

答案4

得分: 0

以下是翻译好的内容:

一种简单的方法是创建一个如下所示的二维数组,然后打印其转置。

  1. 1 2 3 4 5
  2. 10 9 8 7 6
  3. 11 12 13 14 15
  4. 20 19 18 17 16
  5. 21 22 23 24 25

这是一个维度为 LEN x LEN 的二维数组,其中 LEN = 5

上述模式如下:

  1. 模式以 start 值为 1 开始。
  2. 当主循环计数器为偶数时,将分配值给数组的计数器从 start 值开始,逐次增加 LEN 次。最后,它将设置下一行的 startfinal_value_of_the_array_value_assignment_counter - 1 + LEN 开始。
  3. 当主循环计数器为奇数时,将分配值给数组的计数器从 start 值开始,逐次减少 LEN 次。最后,它将设置下一行的 startstart + 1 开始。

上述矩阵的转置如下:

  1. 1 10 11 20 21
  2. 2 9 12 19 22
  3. 3 8 13 18 23
  4. 4 7 14 17 24
  5. 5 6 15 16 25

代码方面,我们可以写成:

  1. public class Main {
  2. public static void main(String[] args) {
  3. final int LEN = 5;
  4. int[][] arr = new int[LEN][LEN];
  5. int start = 1, j, k, col;
  6. for (int i = 0; i < LEN; i++) {
  7. if (i % 2 == 0) {
  8. k = 1;
  9. for (j = start, col = 0; k <= LEN; j++, k++, col++) {
  10. arr[i][col] = j;
  11. }
  12. start = j - 1 + LEN;
  13. } else {
  14. k = 1;
  15. for (j = start, col = 0; k <= LEN; j--, k++, col++) {
  16. arr[i][col] = j;
  17. }
  18. start++;
  19. }
  20. }
  21. // 打印矩阵的转置
  22. for (int r = 0; r < LEN; r++) {
  23. for (int c = 0; c < LEN; c++) {
  24. System.out.print(arr[c][r] + "\t");
  25. }
  26. System.out.println();
  27. }
  28. }
  29. }

输出:

  1. 1 10 11 20 21
  2. 2 9 12 19 22
  3. 3 8 13 18 23
  4. 4 7 14 17 24
  5. 5 6 15 16 25

LEN 的值更改为 10,您将得到以下模式:

  1. 1 20 21 40 41 60 61 80 81 100
  2. 2 19 22 39 42 59 62 79 82 99
  3. 3 18 23 38 43 58 63 78 83 98
  4. 4 17 24 37 44 57 64 77 84 97
  5. 5 16 25 36 45 56 65 76 85 96
  6. 6 15 26 35 46 55 66 75 86 95
  7. 7 14 27 34 47 54 67 74 87 94
  8. 8 13 28 33 48 53 68 73 88 93
  9. 9 12 29 32 49 52 69 72 89 92
  10. 10 11 30 31 50 51 70 71 90 91
英文:

An easy way to do it is to create a 2-D array as shown below and then print its transpose.

  1. 1 2 3 4 5
  2. 10 9 8 7 6
  3. 11 12 13 14 15
  4. 20 19 18 17 16
  5. 21 22 23 24 25

It is a 2-D array of the dimension, LEN x LEN, where LEN = 5.

The above pattern goes like this:

  1. The pattern starts with a start value of 1.
  2. When the main loop counter is an even number, the counter, which assigns a value to the array, starts with start value and increases by one, LEN times. Finally, it sets start for the next row to start with final_value_of_the_array_value_assignment_counter - 1 + LEN.
  3. When the main loop counter is an odd number, the counter, which assigns a value to the array, starts with start value and decreases by one LEN times. Finally, it sets start for the next row to start with start + 1.

The transpose of the above matrix will look like:

  1. 1 10 11 20 21
  2. 2 9 12 19 22
  3. 3 8 13 18 23
  4. 4 7 14 17 24
  5. 5 6 15 16 25

In terms of code, we can write it as:

  1. public class Main {
  2. public static void main(String[] args) {
  3. final int LEN = 5;
  4. int[][] arr = new int[LEN][LEN];
  5. int start = 1, j, k, col;
  6. for (int i = 0; i &lt; LEN; i++) {
  7. if (i % 2 == 0) {
  8. k = 1;
  9. for (j = start, col = 0; k &lt;= LEN; j++, k++, col++) {
  10. arr[i][col] = j;
  11. }
  12. start = j - 1 + LEN;
  13. } else {
  14. k = 1;
  15. for (j = start, col = 0; k &lt;= LEN; j--, k++, col++) {
  16. arr[i][col] = j;
  17. }
  18. start++;
  19. }
  20. }
  21. // Print the transpose of the matrix
  22. for (int r = 0; r &lt; LEN; r++) {
  23. for (int c = 0; c &lt; LEN; c++) {
  24. System.out.print(arr[c][r] + &quot;\t&quot;);
  25. }
  26. System.out.println();
  27. }
  28. }
  29. }

Output:

  1. 1 10 11 20 21
  2. 2 9 12 19 22
  3. 3 8 13 18 23
  4. 4 7 14 17 24
  5. 5 6 15 16 25

Change the value of LEN to 10 and you will get the following pattern:

  1. 1 20 21 40 41 60 61 80 81 100
  2. 2 19 22 39 42 59 62 79 82 99
  3. 3 18 23 38 43 58 63 78 83 98
  4. 4 17 24 37 44 57 64 77 84 97
  5. 5 16 25 36 45 56 65 76 85 96
  6. 6 15 26 35 46 55 66 75 86 95
  7. 7 14 27 34 47 54 67 74 87 94
  8. 8 13 28 33 48 53 68 73 88 93
  9. 9 12 29 32 49 52 69 72 89 92
  10. 10 11 30 31 50 51 70 71 90 91

huangapple
  • 本文由 发表于 2020年4月11日 09:02:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/61150845.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定