标签:int() printf img import for [] ima print class
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner (System.in); int n = sc.nextInt(); int count = 1; int[][] a = new int[n][n]; for(int i = 0 ; i <=a.length/2;i++) { for(int j = i ;j <a.length-i;j++) { a[i][j] = count++; } for(int k = i+1;k<a.length-i;k++) { a[k][a.length-i-1] = count++; } for(int j = a.length-i-2;j>=i;j--) { a[a.length-i-1][j] = count++; } for(int k = a.length-i-2 ; k>=i+1 ; k--) { a[k][i] = count++; } } for(int i = 0 ; i <a.length;i++) { for(int j = 0 ;j<a[i].length;j++) { System.out.printf("%4d",a[i][j]); } System.out.println(); } System.out.println(); } }
实例:
标签:int() printf img import for [] ima print class
原文地址:https://www.cnblogs.com/algorithmvcerhj/p/9090941.html