print the output below for any n.
n = 4
????? 1?????? 2?????? 3?????? 4
????? 12????? 13????? 14????? 5
????? 11????? 16????? 15????? 6
????? 10????? 9?????? 8?????? 7
n = 5
????? 1?????? 2?????? 3?????? 4?????? 5
????? 16????? 17????? 18????? 19????? 6
????? 15????? 24????? 25????? 20????? 7
????? 14????? 23????? 22????? 21????? 8
????? 13????? 12????? 11????? 10????? 9
n = 6
????? 1?????? 2?????? 3?????? 4?????? 5?????? 6
????? 20????? 21????? 22????? 23????? 24????? 7
????? 19????? 32????? 33????? 34????? 25????? 8
????? 18????? 31????? 36????? 35????? 26????? 9
????? 17????? 30????? 29????? 28????? 27????? 10
????? 16????? 15????? 14????? 13????? 12????? 11
n = 1
????? 1
==========我的想法是建立二维数组,先把最左边一列输出,再把第一行输出,然后判断循环输出...分两种情况:向下向左 和 向上向右,最后把最后一列的多余的符号删除。
问题是编译通过,运行报错。
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at problem_n.Exp.shuchu(Main.java:23)---》array[j][0] = x;
at problem_n.Main.main(Main.java:77)---》exp.shuchu(n);package problem_n;
import java.util.Scanner;
/**
*
* @author LPX
*/class Exp{ int i,j,k,l,m,n;
String [][]array;
public void shuchu(int i)
{
String x = "????? ";
String y = "?????? ";
array = new String[i-1][i];
for(j=0;j<=i-1;j++)
array[j][0] = x;
for(j=1;j<=i;j++)
array[0][j] = (j+1)+y;
int l = j+1;
for(k=i;k>0;k--)
{
if((i/k)%2 == 0)
{
for(m=1;m<=k-1;m++,l++)
array[m][k] = (l+1)+y;
for(m=0,n=k-1;m<=k-1;m++,n--,l++)
array[k-1][n] = (l+1)+y;
}
else if((i/k)%2 == 1)
{
for(m=0,n=k-1;m<k-1;m++,n--,l++)
array[k-1][n] = (l+1)+y;
for(m=1;m<=k-1;m++,l++)
array[m][k] = (l+1)+y;
}
}
String p = new String();
String q = y;
for(m=0;m<i-1;m++)
{
p = array[m][i];
String temp = p.replace(q,"");
array[m][i] = temp;
} for(m=0;m<i-1;m++){
for(n=0;n<i;n++){
System.out.printf(array[m][n]);
} } }
}public class Main { /**
* @param args the command line arguments
*/
public static void main(String[] args) { int n;
Scanner sc = new Scanner(System.in);
Exp exp = new Exp();
System.out.println("请输入n的值:");
n = sc.nextInt();
exp.shuchu(n);
}}
n = 4
????? 1?????? 2?????? 3?????? 4
????? 12????? 13????? 14????? 5
????? 11????? 16????? 15????? 6
????? 10????? 9?????? 8?????? 7
n = 5
????? 1?????? 2?????? 3?????? 4?????? 5
????? 16????? 17????? 18????? 19????? 6
????? 15????? 24????? 25????? 20????? 7
????? 14????? 23????? 22????? 21????? 8
????? 13????? 12????? 11????? 10????? 9
n = 6
????? 1?????? 2?????? 3?????? 4?????? 5?????? 6
????? 20????? 21????? 22????? 23????? 24????? 7
????? 19????? 32????? 33????? 34????? 25????? 8
????? 18????? 31????? 36????? 35????? 26????? 9
????? 17????? 30????? 29????? 28????? 27????? 10
????? 16????? 15????? 14????? 13????? 12????? 11
n = 1
????? 1
==========我的想法是建立二维数组,先把最左边一列输出,再把第一行输出,然后判断循环输出...分两种情况:向下向左 和 向上向右,最后把最后一列的多余的符号删除。
问题是编译通过,运行报错。
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at problem_n.Exp.shuchu(Main.java:23)---》array[j][0] = x;
at problem_n.Main.main(Main.java:77)---》exp.shuchu(n);package problem_n;
import java.util.Scanner;
/**
*
* @author LPX
*/class Exp{ int i,j,k,l,m,n;
String [][]array;
public void shuchu(int i)
{
String x = "????? ";
String y = "?????? ";
array = new String[i-1][i];
for(j=0;j<=i-1;j++)
array[j][0] = x;
for(j=1;j<=i;j++)
array[0][j] = (j+1)+y;
int l = j+1;
for(k=i;k>0;k--)
{
if((i/k)%2 == 0)
{
for(m=1;m<=k-1;m++,l++)
array[m][k] = (l+1)+y;
for(m=0,n=k-1;m<=k-1;m++,n--,l++)
array[k-1][n] = (l+1)+y;
}
else if((i/k)%2 == 1)
{
for(m=0,n=k-1;m<k-1;m++,n--,l++)
array[k-1][n] = (l+1)+y;
for(m=1;m<=k-1;m++,l++)
array[m][k] = (l+1)+y;
}
}
String p = new String();
String q = y;
for(m=0;m<i-1;m++)
{
p = array[m][i];
String temp = p.replace(q,"");
array[m][i] = temp;
} for(m=0;m<i-1;m++){
for(n=0;n<i;n++){
System.out.printf(array[m][n]);
} } }
}public class Main { /**
* @param args the command line arguments
*/
public static void main(String[] args) { int n;
Scanner sc = new Scanner(System.in);
Exp exp = new Exp();
System.out.println("请输入n的值:");
n = sc.nextInt();
exp.shuchu(n);
}}
类似这个的小于等于全部换成小于
纵列可以为 0~i-1
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at problem_n.Exp.shuchu(Main.java:39)---》array[k-1][n] = (l+1)+y;
at problem_n.Main.main(Main.java:77)---》exp.shuchu(n);
public class ok{
public static void main(String[] args){
int n;
Scanner sc = new Scanner(System.in);
Exp exp = new Exp();
System.out.println("请输入n的值:");
n = sc.nextInt();
exp.shuchu(n);
}
}
class Exp{
public void shuchu(int n){
int[][] array=new int[n][n];
for(row=0;row<n;++row)
for(col=0;col<n;++col){
array[row][col]=0;
}
row=0;col=0;row_i=0;col_i=1;
for(v=1;v<=n*n;++v){
array[row][col]=v;
if(col+col_i>n-1||row+row_i>n-1||row+row_i<0||col+col_i<0||array[row+row_i][col+col_i]!=0){
tmp=row_i;
row_i=col_i;
col_i=tmp;
col_i=-col_i;
}
row+=row_i;col+=col_i;
}
for(row=0;row<n;++row){
System.out.print("????? ");
for(col=0;col<n-1;++col){
int l=String.valueOf(array[row][col]).length();
if(l==1) System.out.print(array[row][col]+"?????? ");//如果长度是1,则后面加6个?
else System.out.print(array[row][col]+"????? ");//如果长度是2,则后面加5个?,以此类推,不知道有没有自动填充的函数
}
System.out.println(array[row][col]);
}
}
int row,col,v,row_i,col_i,tmp;
}
if(col+col_i>n-1||row+row_i>n-1||row+row_i<0||col+col_i<0||array[row+row_i][col+col_i]!=0){
tmp=row_i;
row_i=col_i;
col_i=tmp;
col_i=-col_i;
}
这段怎么理解