栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

2022.02.13 - NC004.打印回形数

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

2022.02.13 - NC004.打印回形数

文章目录

1. 题目2. 思路

(1) 模拟法 3. 代码

1. 题目

2. 思路 (1) 模拟法

模拟顺时针遍历二维数组的过程。注意!这道题是ACM模式,需要自己从控制台接收输入的参数。 3. 代码

import java.util.Scanner;

public class Main {
    public static int width;
    public static int height;
    public static int[][] array;
    public static int left;
    public static int right;
    public static int up;
    public static int down;
    public static int curNum;

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        width = scanner.nextInt();
        height = scanner.nextInt();
        array = new int[height][width];
        left = -1;
        right = width;
        up = -1;
        down = height;
        curNum = 0;
        moveRight();
        for (int[] ints : array) {
            for (int anInt : ints) {
                System.out.print(anInt);
            }
            System.out.println();
        }
    }

    public static void moveRight() {
        if (left + 1 == right) {
            return;
        }
        for (int i = left + 1; i < right; i++) {
            array[up + 1][i] = curNum;

        }
        curNum++;
        up++;
        moveDown();
    }

    public static void moveDown() {
        if (up + 1 == down) {
            return;
        }
        for (int i = up + 1; i < down; i++) {
            array[i][right - 1] = curNum;

        }
        curNum++;
        right--;
        moveLeft();
    }

    public static void moveLeft() {
        if (right - 1 == left) {
            return;
        }
        for (int i = right - 1; i > left; i--) {
            array[down - 1][i] = curNum;

        }
        curNum++;
        down--;
        moveUp();
    }

    public static void moveUp() {
        if (down - 1 == up) {
            return;
        }
        for (int i = down - 1; i > up; i--) {
            array[i][left + 1] = curNum;

        }
        curNum++;
        left++;
        moveRight();
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/736830.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号