Java 回形数

import java.util.InputMismatchException;
import java.util.Scanner;

/**
 * 回形数。
 * 要求:输入一个数,输出对应的回形数组。
 * 如:输入:3
 * 返回:
 * 1 2 3
 * 8 9 4
 * 7 6 5
 *
 * 输入:4
 * 返回:
 * 1  2  3  4
 * 12 13 14 5
 * 11 16 15 6
 * 10 9  8  7
 */
public class ClipNumber {
    public static void main(String[] args) {
        do {
            try {
                Scanner scanner = new Scanner(System.in);
                System.out.println("Please input a positive integer (enter 0 to exit):");
                int n = scanner.nextInt();
                if (n == 0) {
                    return;
                }
                int[][] array = new int[n][n];
                int i = 0;
                int j = 0;
                int count = 1;
                int direction = 1;
                int minI = 0;
                int minJ = 0;
                int maxI = n - 1;
                int maxJ = n - 1;
                while (count <= n * n) {
                    switch (direction) {
                        case 1:
                            if (minJ <= j && j <= maxJ) {
                                array[minI][j++] = count++;
                            } else {
                                minI++;
                                i++;
                                direction = 2;
                                j--;
                            }
                            break;
                        case 2:
                            if (minI <= i && i <= maxI) {
                                array[i++][maxJ] = count++;
                            } else {
                                maxJ--;
                                j--;
                                direction = 3;
                                i--;
                            }
                            break;
                        case 3:
                            if (minJ <= j && j <= maxJ) {
                                array[maxI][j--] = count++;
                            } else {
                                maxI--;
                                i--;
                                direction = 4;
                                j++;
                            }
                            break;
                        case 4:
                            if (minI <= i && i <= maxI) {
                                array[i--][minJ] = count++;
                            } else {
                                minJ++;
                                j++;
                                direction = 1;
                                i++;
                            }
                            break;
                        default:
                            break;
                    }
                }
                int digits = String.valueOf(n * n).length();
                for (int[] arrayI : array) {
                    for (int intJ : arrayI) {
                        System.out.printf("%1$-" + (digits + 1) + "s", intJ);
                    }
                    System.out.println();
                }
            } catch (InputMismatchException | NegativeArraySizeException e) {
                System.out.println("The input is not a positive integer.");
            }
        } while (true);

    }
}

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值