现在学做网站赚钱吗,视频网站源码下载,广东新闻头条最新消息,网络工程专业毕业生设计解题思路#xff1a;
通过定义四条边界#xff1b;top,left,right,bottom,来循环#xff0c;当topbottomleftright的时候循环终止 循环结束的条件#xff1a; 链接#xff1a;登录—专业IT笔试面试备考平台_牛客网 来源#xff1a;牛客网
题目描述…解题思路
通过定义四条边界top,left,right,bottom,来循环当topbottomleftright的时候循环终止 循环结束的条件 链接登录—专业IT笔试面试备考平台_牛客网 来源牛客网
题目描述
给你一个整数n按要求输出n∗n的回型矩阵
输入描述:
输入一行包含一个整数n1n19
输出描述:
输出n行每行包含n个正整数.
示例1
输入
4
输出
1 2 3 4
12 13 14 5
11 16 15 6
10 9 8 7
代码
package cs2;import java.util.Scanner;public class T12 {public static void main(String[] args) {Scanner scannernew Scanner(System.in);int nscanner.nextInt();int [][]arrnew int[n][n];int left0;int rightn-1;int top0;int bottomn-1;int sum1;while (leftrighttopbottom){//从左到右//这个循环写的是top边界它要不断的往下去循环所以这里数组的行是top里面的值是列//列是可变的所以列当时top的值是0所以要给top赋值让它一只往下加//当top的值大于bottom的时候跳出while循环遍历数组所有元素输出;for (int i left; i right ; i) {arr[top][i]sum;}top;//从上往下for (int i top; i bottom ; i) {arr[i][right]sum;}right--;for (int i right; i left ; i--) {arr[bottom][i]sum;}bottom--;for (int i bottom; i top ; i--) {arr[i][left]sum;}left;}for (int i 0; i n ; i) {for (int j 0; j n ; j) {System.out.print(arr[i][j] );}System.out.println();}}
}