Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Java

Chương trình Java in mẫu xoắn ốc số (Spiral Pattern)

Giới thiệu

Trong bài viết này, chúng ta sẽ tìm hiểu cách in mẫu xoắn ốc số (spiral pattern of numbers). Mẫu được tạo bằng nhiều vòng lặp for lồng nhau và lệnh in.

Minh họa

Đầu vào:

Enter the size : 5

Đầu ra:

The spiral pattern 
5 5 5 5 5 5 5 5 5 5
4 4 4 4 4 4 4 5 5
4 3 3 3 3 3 4 5 5
4 3 2 2 2 3 4 5 5
4 3 2 1 2 3 4 5 5
4 3 2 2 2 3 4 5 5
4 3 3 3 3 3 4 5 5
4 4 4 4 4 4 4 5 5
5 5 5 5 5 5 5 5 5 5

Thuật toán

  1. Bắt đầu
  2. Khai báo 3 số nguyên: i, my_pattern_size, my_input
  3. Đọc giá trị từ người dùng hoặc định nghĩa trước
  4. Gán 2 * my_input - 1 cho my_pattern_size
  5. Duyệt 2 vòng lặp for lồng nhau
  6. In giá trị Math.max(Math.abs(i - my_input), Math.abs(j - my_input)) + 1
  7. Hiển thị kết quả
  8. Kết thúc

Ví dụ 1: Nhập từ bàn phím (Scanner)

import java.util.Scanner;
import java.lang.Math;

public class SpiralPattern {
    public static void main(String args[]) {
        int my_input, i, j, my_pattern_size;
        System.out.println("Required packages have been imported");
        Scanner my_scanner = new Scanner(System.in);
        System.out.println("A reader object has been defined ");
        System.out.print("Enter the size : ");
        my_input = my_scanner.nextInt();
        System.out.print("The spiral pattern ");
        System.out.println();
        
        my_pattern_size = 2 * my_input - 1;
        for (i = 1; i <= my_pattern_size; i++) {
            for (j = 1; j <= my_pattern_size; j++) {
                System.out.print(Math.max(Math.abs(i - my_input), Math.abs(j - my_input)) + 1 + " ");
            }
            System.out.println();
        }
    }
}

Đầu ra

Required packages have been imported
A reader object has been defined
Enter the size : 5
The spiral pattern 
5 5 5 5 5 5 5 5 5 5
4 4 4 4 4 4 4 5 5
4 3 3 3 3 3 4 5 5
4 3 2 2 2 3 4 5 5
4 3 2 1 2 3 4 5 5
4 3 2 2 2 3 4 5 5
4 3 3 3 3 3 4 5 5
4 4 4 4 4 4 4 5 5
5 5 5 5 5 5 5 5 5 5

Ví dụ 2: Giá trị định nghĩa trước

import java.lang.Math;

public class SpiralPattern {
    public static void main(String args[]) {
        int my_input, i, j, my_pattern_size;
        System.out.println("Required packages have been imported");
        my_input = 5;
        System.out.println("The size is defined as " + my_input);
        System.out.print("The spiral pattern ");
        System.out.println();
        
        my_pattern_size = 2 * my_input - 1;
        for (i = 1; i <= my_pattern_size; i++) {
            for (j = 1; j <= my_pattern_size; j++) {
                System.out.print(Math.max(Math.abs(i - my_input), Math.abs(j - my_input)) + 1 + " ");
            }
            System.out.println();
        }
    }
}

Đầu ra

Required packages have been imported
The size is defined as 5
The spiral pattern 
5 5 5 5 5 5 5 5 5 5
4 4 4 4 4 4 4 5 5
4 3 3 3 3 3 4 5 5
4 3 2 2 2 3 4 5 5
4 3 2 1 2 3 4 5 5
4 3 2 2 2 3 4 5 5
4 3 3 3 3 3 4 5 5
4 4 4 4 4 4 4 5 5
5 5 5 5 5 5 5 5 5 5