红橙作伴


私信TA

用户名:13782239594

访问量:2076

签 名:

等  级
排  名 2943
经  验 2007
参赛次数 2
文章发表 3
年  龄 0
在职情况 学生
学  校 河南农业大学
专  业

  自我简介:

TA的其他文章

解题思路:
没啥思路,就一个二维数组
注意事项:
PE了好几把,难受,mmp审题不清好难受啊
参考代码:

import java.util.*;

public class Main

{

public static void main(String args[])

{

Scanner sc = new Scanner(System.in);

int t,x,y;

int n;

int a[][] = new int [101][101];

while(sc.hasNextInt())

{

n = sc.nextInt();

for(int i=0;i < 101;i++)

{

for(int j = 0;j < 101;j++)

{

a[i][j]=0;

}

}

t=2;

x=0;

y=n-1;

a[0][n-1]=1;

while(t <= n * n)

{

while(x + 1 < n && a[x+1][y] == 0)

{

a[x+1][y] = t++;

x++;

}

while(y - 1 >=0 && a[x][y-1] == 0)

{

a[x][y-1] = t++;

y--;

}

while(x - 1 >= 0 && a[x-1][y] == 0)

{

a[x-1][y] = t++;

x--;

}

while(y + 1 < n && a[x][y+1] == 0)

{

a[x][y+1] = t++;

y++;

}

}

for(int i = 0;i < n;i++)

{

for(int j = 0;j < n;j++)

{

System.out.print(a[i][j]);

if(j < n - 1)

System.out.print(" ");

if(j == n  - 1)

System.out.println();

}

}

System.out.println();

}

}

}


 

0.0分

1 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区