黑马程序员技术交流社区
标题:
分享昨晚做了快三个小时的题目
[打印本页]
作者:
quq947115876
时间:
2014-9-14 10:10
标题:
分享昨晚做了快三个小时的题目
9、 写一方法,打印等长的二维数组,要求从1开始的自然数由方阵的最外圈向内螺旋方式地顺序排列。
* 解题思路:
* 想象成一个人绕圈行走,所以要有方向,一个方向走到头,该方向的步数就要减少一步,直到循环足够退出。
* 1.循环时的四个边界
* 2.方向变换的条件
* 3.退出条件
public class test9 {
//开始循环的数从1开始
public static int beginNum = 1;
//数组的大小
public static final int aryNum = 10;
//初始化行走方向,向左。 这里1表示向左,2表示向下,3表示向右,4表示向上
public static final int direction = 1;
public static void main(String[] args) {
int[][] num = new int[aryNum][aryNum];
setNum(0, 0, 0, aryNum, 0, aryNum, direction, num);
for(int a = 0;a<aryNum;a++){
for(int b=0;b < aryNum;b++){
System.out.print(num[a][b]+"\t");
}
System.out.println("\n");
}
}
/*
* x:数组的一维坐标
* y:数组的二维坐标
* boundaryLeftY:数组的左边界,从0开始
* boundaryRightY:数组右边界,限制为数组的长度减一
* boundaryTopX:数组的上边界,从1开始,因为开始循环的时候最上方就已经占据了一行
* boundaryBottomX:数组的下边界,限制为数组的长度减一
* direction:行走的方向
* num:数组对象
*/
public static void setNum(int x,int y,int boundaryLeftY,int boundaryRightY,int boundaryTopX,
int boundaryBottomX,int direction,int[][] num){
//当累加的量超过数组的平方大小就退出
if(beginNum > aryNum*aryNum){
return;
}
System.out.println("x="+x+" y="+y+" direction="+direction+" boundaryBottomX="+boundaryBottomX);
switch (direction) {
//向左方向
case 1:
//不断向该方向填充数据
num[x][y++] = beginNum++;
//当y累加超过该方向的限制的时候就换方向
if(y > boundaryRightY - 1){
//y已经超过数据界限,所以必须对它重新赋值
y = boundaryRightY - 1;
//为下个方向累加
x++;
//换方向
setNum(x, y, boundaryLeftY, boundaryRightY - 1,boundaryTopX, boundaryBottomX,2, num);
}
//不需要变换方向
setNum(x, y, boundaryLeftY,boundaryRightY,boundaryTopX, boundaryBottomX, 1 , num);
break;
//向下方向
case 2:
num[x++][y] = beginNum++;
if(x > boundaryBottomX - 1){
x = boundaryBottomX - 1;
y--;
setNum(x, y, boundaryLeftY, boundaryRightY,boundaryTopX, boundaryBottomX - 1,3, num);
}
setNum(x, y, boundaryLeftY,boundaryRightY,boundaryTopX, boundaryBottomX, 2 , num);
break;
//向右方向
case 3:
num[x][y--] = beginNum++;
if(y < boundaryLeftY){
y = boundaryLeftY;
x--;
setNum(x, y, boundaryLeftY+1, boundaryRightY,boundaryTopX, boundaryBottomX,4, num);
}
setNum(x, y, boundaryLeftY,boundaryRightY,boundaryTopX, boundaryBottomX, 3 , num);
break;
//向上方向
case 4:
num[x--][y] = beginNum++;
if(x < boundaryTopX + 1){
x = boundaryTopX + 1;
y++;
setNum(x, y, boundaryLeftY, boundaryRightY,boundaryTopX+1, boundaryBottomX,1, num);
}
setNum(x, y, boundaryLeftY,boundaryRightY,boundaryTopX, boundaryBottomX, 4 , num);
break;
default:
break;
}
}
}
复制代码
刚开始对这道题真是无从下手,想了好久,做这道题真的是觉得项目不是做出来,而是调试出来的。
代码中有不足之处请大家多提出来,互相学习。
作者:
菜鸟一号
时间:
2014-9-14 10:30
这是交通灯的?
作者:
hejinzhong
时间:
2014-9-14 11:17
本帖最后由 hejinzhong 于 2014-9-14 11:19 编辑
/*
* 螺旋矩阵是指一个呈螺旋状的矩阵,它的数字由第一行开始到右边不断变大,向下变大,
* 向左变大,向上变大,如此循环。
*/
public class Circle {
/**
* 接收三个参数,矩阵的行和列,以及起始数字
*/
public static void main(String[] args) {
helix(4,4,1);
}
/*
* 这个是左上角开始,顺时针转的螺旋
*
* 思想:先给最外层一圈赋值,再给第二圈赋值,直至结束
* 循环次数由行和列中较小一个决定,即为大于--较小者一半的---最小整数
*/
public static void helix(int w,int h,int start){
int[][] arr = new int[w][h];
int left=0,top=0,right=w-1,botton=h-1;
int max = w>h?(h+1)/2:(w+1)/2;
for(int i=0;i<max;i++){
for(int x=left;x<=right;x++){
arr[top][x] = start++;
}
top++;
for(int y=top;y<=botton;y++){
arr[y][right] = start++;
}
right--;
for(int z=right;z>=left;z--){
arr[botton][z]=start++;
}
botton--;
for(int k=botton;k>=top;k--){
arr[k][left]=start++;
}
left++;
}
for(int i=0;i<w;i++){
for(int j=0;j<h;j++){
System.out.print(arr[i][j]+"\t");
}
System.out.println();
}
}
}
复制代码
作者:
tommy
时间:
2014-9-14 11:29
package com.lianxi.test02;
import java.util.Scanner;
public class Test9 {
/*
* 写一方法,打印等长的二维数组,要求从1开始的自然数由方阵的最外圈
* 向内螺旋方式地顺序排列。
*/
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int num = scanner.nextInt();
method(num);
}
public static void method(int num)
{
int count = 0;
int[][] arr = new int[num][num];
int n = 0;
if(num%2 == 0)
n = num/2;
else
n = num/2 + 1;
for(int i = 0;i<n;i++)
{
for(int j = i;j<num-i;j++)
{
arr[i][j] = ++count;
}
for(int j = i+1;j<num-i;j++)
{
arr[j][num-1-i] = ++count;
}
for(int j = num-i-2;j>=i;j--)
{
arr[num-1-i][j] = ++count;
}
for(int j = num-i-2;j>=i+1;j--)
{
arr[j][i] = ++count;
}
}
for(int i = 0;i<num;i++)
{
for(int j = 0;j<num;j++)
{
System.out.print(arr[i][j]+"\t");
}
System.out.println();
}
}
}
作者:
648947721
时间:
2014-9-14 12:44
学习学习
作者:
夜半风
时间:
2014-9-14 16:34
略屌 还没仔细看 回头仔细研究下
作者:
郑飞
时间:
2014-9-14 18:50
呵呵 第一次做某些题目 确实很难 不过做一题会一类 加油
作者:
不轻易
时间:
2014-9-14 20:26
向楼主学习啊~~
作者:
王会涛
时间:
2014-12-13 18:55
用递归试试,没这么麻烦V
作者:
l763631191
时间:
2014-12-16 01:17
不错继续坚持
作者:
祁祯祥
时间:
2014-12-18 20:53
这道题原来如此啊
作者:
地狱里的帅灵魂
时间:
2015-9-5 14:44
好叼,感觉眼前一片黑
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2