要求:存储和输出nXm的螺旋数组,其中n和m为大于0的整数。
以下是一些螺旋数组的示例:
1 2 3 4 1 2 3 4 5
12 13 14 5 14 15 16 17 6
11 16 15 6 13 20 19 18 7
10 9 8 7 12 11 10 9 8
4X4螺旋数组 4X5螺旋数组
我的程序(一)如下:public class arrayTest1 { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int n=4;
int m=4;
int [][]arr=new int [n][m];
final int RIGHT=0;
final int LEFT=1;
final int DOWN=2;
final int UP=3;
int step=1;
int toward=RIGHT;
int row=0;
int col=0;
arr[0][0]=1;
while(step<n*m){
switch (toward){
case RIGHT:
col++;
if(col>=m){
col--;
toward=DOWN;
continue;
}
else if(arr[row][col]!=0){
col--;
toward=DOWN;
continue;
}
break;
case DOWN:
row++;
if(row>=n){
row--;
toward=LEFT;
continue;
}
else if(arr[row][col]!=0){
row--;
toward=DOWN;
continue;
}
break;
case LEFT:
col--;
if(col<0){
col++;
toward=UP;
continue;
}
else if(arr[row][col]!=0){
col++;
toward=DOWN;
continue;
}
break;
case UP:
row--;
if(row<0){
row++;
toward=RIGHT;
continue;
}
else if(arr[row][col]!=0){
row++;
toward=DOWN;
continue;
}
break;
}
step++;
arr[row][col]=step;
}
for (int i = 0; i < arr.length; i++){
for(int j = 0; j<arr[i].length;j++){
if(arr[i][j]<10){
System.out.print(arr[i][j]);
System.out.print(" ");
}
else {
System.out.print(arr[i][j]);
System.out.append(" ");
}
}
System.out.println();
}
}}
运行没有反应,没有任何输出.我的程序(二)如下:public class arrayTest1 { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int n=4;
int m=4;
int [][]arr=new int [n][m];
final int RIGHT=0;
final int LEFT=1;
final int DOWN=2;
final int UP=3;
int step=1;
int toward=RIGHT;
int row=0;
int col=0;
arr[0][0]=1;
while(step<n*m){
switch (toward){
case RIGHT:
col++;
if(col>=m||arr[row][col]!=0){
col--;
toward=DOWN;
continue;
} break;
case DOWN:
row++;
if(row>=n||arr[row][col]!=0){
row--;
toward=LEFT;
continue;
} break;
case LEFT:
col--;
if(col<0||arr[row][col]!=0){
col++;
toward=UP;
continue;
} break;
case UP:
row--;
if(row<0||arr[row][col]!=0){
row++;
toward=RIGHT;
continue;
} break;
}
step++;
arr[row][col]=step;
}
for (int i = 0; i < arr.length; i++){
for(int j = 0; j<arr[i].length;j++){
if(arr[i][j]<10){
System.out.print(arr[i][j]);
System.out.print(" ");
}
else {
System.out.print(arr[i][j]);
System.out.append(" ");
}
}
System.out.println();
}
}}能够输出结果.
请问这两个程序实质上有区别吗?为什么第一个运行会毫无反应?...
以下是一些螺旋数组的示例:
1 2 3 4 1 2 3 4 5
12 13 14 5 14 15 16 17 6
11 16 15 6 13 20 19 18 7
10 9 8 7 12 11 10 9 8
4X4螺旋数组 4X5螺旋数组
我的程序(一)如下:public class arrayTest1 { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int n=4;
int m=4;
int [][]arr=new int [n][m];
final int RIGHT=0;
final int LEFT=1;
final int DOWN=2;
final int UP=3;
int step=1;
int toward=RIGHT;
int row=0;
int col=0;
arr[0][0]=1;
while(step<n*m){
switch (toward){
case RIGHT:
col++;
if(col>=m){
col--;
toward=DOWN;
continue;
}
else if(arr[row][col]!=0){
col--;
toward=DOWN;
continue;
}
break;
case DOWN:
row++;
if(row>=n){
row--;
toward=LEFT;
continue;
}
else if(arr[row][col]!=0){
row--;
toward=DOWN;
continue;
}
break;
case LEFT:
col--;
if(col<0){
col++;
toward=UP;
continue;
}
else if(arr[row][col]!=0){
col++;
toward=DOWN;
continue;
}
break;
case UP:
row--;
if(row<0){
row++;
toward=RIGHT;
continue;
}
else if(arr[row][col]!=0){
row++;
toward=DOWN;
continue;
}
break;
}
step++;
arr[row][col]=step;
}
for (int i = 0; i < arr.length; i++){
for(int j = 0; j<arr[i].length;j++){
if(arr[i][j]<10){
System.out.print(arr[i][j]);
System.out.print(" ");
}
else {
System.out.print(arr[i][j]);
System.out.append(" ");
}
}
System.out.println();
}
}}
运行没有反应,没有任何输出.我的程序(二)如下:public class arrayTest1 { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int n=4;
int m=4;
int [][]arr=new int [n][m];
final int RIGHT=0;
final int LEFT=1;
final int DOWN=2;
final int UP=3;
int step=1;
int toward=RIGHT;
int row=0;
int col=0;
arr[0][0]=1;
while(step<n*m){
switch (toward){
case RIGHT:
col++;
if(col>=m||arr[row][col]!=0){
col--;
toward=DOWN;
continue;
} break;
case DOWN:
row++;
if(row>=n||arr[row][col]!=0){
row--;
toward=LEFT;
continue;
} break;
case LEFT:
col--;
if(col<0||arr[row][col]!=0){
col++;
toward=UP;
continue;
} break;
case UP:
row--;
if(row<0||arr[row][col]!=0){
row++;
toward=RIGHT;
continue;
} break;
}
step++;
arr[row][col]=step;
}
for (int i = 0; i < arr.length; i++){
for(int j = 0; j<arr[i].length;j++){
if(arr[i][j]<10){
System.out.print(arr[i][j]);
System.out.print(" ");
}
else {
System.out.print(arr[i][j]);
System.out.append(" ");
}
}
System.out.println();
}
}}能够输出结果.
请问这两个程序实质上有区别吗?为什么第一个运行会毫无反应?...
解决方案 »
- Collection与iterator()的关系
- 带有main函数,用命令行传参的C语言程序,在java中如何调用
- 请教大家,我想通过JDBC访问一台安全性设置为Windows验证的SQL Server,请问如何写创建数据库连接的程序呀?
- 请问多线程的例子,明明是输出偶数的,怎么回事?在线等,解决了就加分!!
- 請問那們大俠寫過GUI控件
- 提示找不到方法!
- 怎样让一台机器通过 另一台拨号上网的机器来上网呀!!!!!(求斑竹别删,急呀!!)
- 关于哈希(hash)表
- 当发布的程序中含有数据库的使用时,...?
- 关于《Thinking in Java》的问题
- 请问MYSQL 08S01错误怎么解决,谢谢
- PLSQL语句调用存储过程出错了,急求答案
楼主【hiker_1】截止到2008-07-08 12:16:01的历史汇总数据(不包括此帖):
发帖的总数量:0 发帖的总分数:0 每贴平均分数:0
回帖的总数量:6 得分贴总数量:0 回帖的得分率:0%
结贴的总数量:0 结贴的总分数:0
无满意结贴数:0 无满意结贴分:0
未结的帖子数:0 未结的总分数:0
结贴的百分比:---------------------结分的百分比:---------------------
无满意结贴率:---------------------无满意结分率:---------------------
如何结贴请参考这里:http://topic.csdn.net/u/20080501/09/ef7ba1b3-6466-49f6-9d92-36fe6d471dd1.html
{
private int x;
private int y;
public LuoXuanShuZu(int x, int y)
{
this.x = x;
this.y = y;
}
public void show()
{
int[][] result = new int[this.y][this.x];
int count = 1;
int x = 0;
int y = 0;
char direct;
result[0][0] = count;
while(count < this.x * this.y)
{
count++;
direct = getDirect(result, x, y);
switch(direct)
{
case 'L':
x--;
break;
case 'R':
x++;
break;
case 'U':
y--;
break;
case 'D':
y++;
break;
}
result[y][x] = count;
}
print(result);
}
private char getDirect(int[][] value, int x, int y)
{
int xBound = value[y].length;
int yBound = value.length;
char directL = (x - 1) < 0 || value[y][x - 1] != 0 ? 'I' : 'A';
char directR = (x + 1) >= xBound || value[y][x + 1] != 0 ? 'I' : 'A';
char directU = (y - 1) < 0 || value[y - 1][x] != 0 ? 'I' : 'A';
char directD = (y + 1) >= yBound || value[y + 1][x] != 0 ? 'I' : 'A';
if('I' == directL && 'I' != directU)
{
return 'U';
}
if('I' == directU && 'I' != directR)
{
return 'R';
}
if('I' == directR && 'I' != directD)
{
return 'D';
}
return 'L';
}
private void print(int[][] result)
{
for(int[] ele1: result)
{
for(int ele2: ele1)
{
System.out.print(ele2 + " ");
}
System.out.println();
}
}
public static void main(String[] args)
{
LuoXuanShuZu ele = new LuoXuanShuZu(10, 10);
ele.show();
}
}我这个可以实现.
{
private int x;
private int y;
public LuoXuanShuZu(int x, int y)
{
this.x = x;
this.y = y;
}
public void show()
{
int[][] result = new int[this.y][this.x];
int count = 1;
int x = 0;
int y = 0;
char direct;
result[0][0] = count;
while(count < this.x * this.y)
{
count++;
direct = getDirect(result, x, y);
switch(direct)
{
case 'L':
x--;
break;
case 'R':
x++;
break;
case 'U':
y--;
break;
case 'D':
y++;
break;
}
result[y][x] = count;
}
print(result);
}
private char getDirect(int[][] value, int x, int y)
{
int xBound = value[y].length;
int yBound = value.length;
char directL = (x - 1) < 0 || value[y][x - 1] != 0 ? 'I' : 'A';
char directR = (x + 1) >= xBound || value[y][x + 1] != 0 ? 'I' : 'A';
char directU = (y - 1) < 0 || value[y - 1][x] != 0 ? 'I' : 'A';
char directD = (y + 1) >= yBound || value[y + 1][x] != 0 ? 'I' : 'A';
if('I' == directL && 'I' != directU)
{
return 'U';
}
if('I' == directU && 'I' != directR)
{
return 'R';
}
if('I' == directR && 'I' != directD)
{
return 'D';
}
return 'L';
}
private void print(int[][] result)
{
for(int[] ele1: result)
{
for(int ele2: ele1)
{
System.out.print(ele2 + " ");
}
System.out.println();
}
}
public static void main(String[] args)
{
LuoXuanShuZu ele = new LuoXuanShuZu(10, 10);
ele.show();
}
}我这个可以实现.