例子一:
(3,5)表示3行5列
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
把一个数组按上面的形式显示出来后,按规则输出
结果输出:1,2,3,4,5,9,13,12,11,6,7,8
例子二:
(5,3)表示5行3列
1 2 3
4 5 6
7 8 9
10 11 12
13 14 15
结果输出:1,2,3,5,7,4
补充说明:几行几列不定
不知道大神能不能看懂我举的例子,大家来看看啦。。。。。。。。
6 个解决方案
#1
在while 循环里分4个方向走,当要走的下一个位置已经走过,则结束
#2

#3
public static void main(String[] args) {
int n = 3;
int m = 5;
int curRow = 0;
int curCol = 0;
Integer[][] array = new Integer[n][m];
for (int i = 0; i < 15; i++) {
array[i / m][i % m] = i + 1;
}
List<Integer> list = new ArrayList<Integer>();
// for (int i = 0; i < 3; i++) {
// for (int j = 0; j < 5; j++) {
// System.out.print(array[i][j] +" ");
// }
// }
boolean success = false;
// 左往右
while (curCol < m) {
if (!list.contains(array[curRow][curCol])) {
list.add(array[curRow][curCol]);
curCol++;
} else {
success = true;
break;
}
}
if (success == true) {
System.out.println(list);
Scanner scan = new Scanner(System.in);
scan.next();
}
curCol--;
// 斜向左
while ((++curRow) < n & (--curCol) >= 0) {
if (!list.contains(array[curRow][curCol])) {
list.add(array[curRow][curCol]);
} else {
success = true;
break;
}
}
if (success == true) {
System.out.println(list);
Scanner scan = new Scanner(System.in);
scan.next();
}
curRow--;
curCol++;
// 向左
while ((--curCol) >= 0) {
if (!list.contains(array[curRow][curCol])) {
list.add(array[curRow][curCol]);
} else {
success = true;
break;
}
}
if (success == true) {
System.out.println(list);
Scanner scan = new Scanner(System.in);
scan.next();
}
if (curCol < 0)
curCol++;
// 向上
curRow--;
if (!list.contains(array[curRow][curCol])) {
list.add(array[curRow][curCol]);
} else {
success = true;
}
if (success == true) {
System.out.println(list);
Scanner scan = new Scanner(System.in);
scan.next();
}
// 向右
while ((++curCol) < m) {
if (!list.contains(array[curRow][curCol])) {
list.add(array[curRow][curCol]);
} else {
success = true;
break;
}
}
if (success == true) {
System.out.println(list);
Scanner scan = new Scanner(System.in);
scan.next();
}
}
int n = 3;
int m = 5;
int curRow = 0;
int curCol = 0;
Integer[][] array = new Integer[n][m];
for (int i = 0; i < 15; i++) {
array[i / m][i % m] = i + 1;
}
List<Integer> list = new ArrayList<Integer>();
// for (int i = 0; i < 3; i++) {
// for (int j = 0; j < 5; j++) {
// System.out.print(array[i][j] +" ");
// }
// }
boolean success = false;
// 左往右
while (curCol < m) {
if (!list.contains(array[curRow][curCol])) {
list.add(array[curRow][curCol]);
curCol++;
} else {
success = true;
break;
}
}
if (success == true) {
System.out.println(list);
Scanner scan = new Scanner(System.in);
scan.next();
}
curCol--;
// 斜向左
while ((++curRow) < n & (--curCol) >= 0) {
if (!list.contains(array[curRow][curCol])) {
list.add(array[curRow][curCol]);
} else {
success = true;
break;
}
}
if (success == true) {
System.out.println(list);
Scanner scan = new Scanner(System.in);
scan.next();
}
curRow--;
curCol++;
// 向左
while ((--curCol) >= 0) {
if (!list.contains(array[curRow][curCol])) {
list.add(array[curRow][curCol]);
} else {
success = true;
break;
}
}
if (success == true) {
System.out.println(list);
Scanner scan = new Scanner(System.in);
scan.next();
}
if (curCol < 0)
curCol++;
// 向上
curRow--;
if (!list.contains(array[curRow][curCol])) {
list.add(array[curRow][curCol]);
} else {
success = true;
}
if (success == true) {
System.out.println(list);
Scanner scan = new Scanner(System.in);
scan.next();
}
// 向右
while ((++curCol) < m) {
if (!list.contains(array[curRow][curCol])) {
list.add(array[curRow][curCol]);
} else {
success = true;
break;
}
}
if (success == true) {
System.out.println(list);
Scanner scan = new Scanner(System.in);
scan.next();
}
}
#4
刚刚用命令模式写了一个,
发现比较长,论坛不能添加附件,代码只贴主程序部分
楼上的很简略,按面向过程一步步求解就可以
这个也没什么算法
按一定方式算二维坐标值
将遇到的值加入到list里面
每次加入先判断list里面有没有
没有则加入,有则终止
发现比较长,论坛不能添加附件,代码只贴主程序部分
楼上的很简略,按面向过程一步步求解就可以
这个也没什么算法
按一定方式算二维坐标值
将遇到的值加入到list里面
每次加入先判断list里面有没有
没有则加入,有则终止
public class CommandTest {
public static void main(String[] args) {
int[][] arrays = {
{1,2,3,4,5},
{6,7,8,9,10},
{11,12,13,14,15},
{16,17,18,19,20},
};
DataModel dataModel = new DataModel(arrays, 0, 0);
Command toE = new CommandMoveToE(dataModel);
Command toSW = new CommandMoveToSW(dataModel);
Command toN = new CommandMoveToN(dataModel);
Command toW = new CommandMoveToW(dataModel);
MoveCase moveCase = new MoveCase();
moveCase.addCommand(toE);
moveCase.addCommand(toSW);
moveCase.addCommand(toW);
moveCase.addCommand(toN);
moveCase.execute();
}
}
#5
我补充一点,数组是一维数组
数组:int arr[]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};而且里面的15个元素不定,也有可能是18个元素,那么就是6行3列等
#6

#1
在while 循环里分4个方向走,当要走的下一个位置已经走过,则结束
#2

#3
public static void main(String[] args) {
int n = 3;
int m = 5;
int curRow = 0;
int curCol = 0;
Integer[][] array = new Integer[n][m];
for (int i = 0; i < 15; i++) {
array[i / m][i % m] = i + 1;
}
List<Integer> list = new ArrayList<Integer>();
// for (int i = 0; i < 3; i++) {
// for (int j = 0; j < 5; j++) {
// System.out.print(array[i][j] +" ");
// }
// }
boolean success = false;
// 左往右
while (curCol < m) {
if (!list.contains(array[curRow][curCol])) {
list.add(array[curRow][curCol]);
curCol++;
} else {
success = true;
break;
}
}
if (success == true) {
System.out.println(list);
Scanner scan = new Scanner(System.in);
scan.next();
}
curCol--;
// 斜向左
while ((++curRow) < n & (--curCol) >= 0) {
if (!list.contains(array[curRow][curCol])) {
list.add(array[curRow][curCol]);
} else {
success = true;
break;
}
}
if (success == true) {
System.out.println(list);
Scanner scan = new Scanner(System.in);
scan.next();
}
curRow--;
curCol++;
// 向左
while ((--curCol) >= 0) {
if (!list.contains(array[curRow][curCol])) {
list.add(array[curRow][curCol]);
} else {
success = true;
break;
}
}
if (success == true) {
System.out.println(list);
Scanner scan = new Scanner(System.in);
scan.next();
}
if (curCol < 0)
curCol++;
// 向上
curRow--;
if (!list.contains(array[curRow][curCol])) {
list.add(array[curRow][curCol]);
} else {
success = true;
}
if (success == true) {
System.out.println(list);
Scanner scan = new Scanner(System.in);
scan.next();
}
// 向右
while ((++curCol) < m) {
if (!list.contains(array[curRow][curCol])) {
list.add(array[curRow][curCol]);
} else {
success = true;
break;
}
}
if (success == true) {
System.out.println(list);
Scanner scan = new Scanner(System.in);
scan.next();
}
}
int n = 3;
int m = 5;
int curRow = 0;
int curCol = 0;
Integer[][] array = new Integer[n][m];
for (int i = 0; i < 15; i++) {
array[i / m][i % m] = i + 1;
}
List<Integer> list = new ArrayList<Integer>();
// for (int i = 0; i < 3; i++) {
// for (int j = 0; j < 5; j++) {
// System.out.print(array[i][j] +" ");
// }
// }
boolean success = false;
// 左往右
while (curCol < m) {
if (!list.contains(array[curRow][curCol])) {
list.add(array[curRow][curCol]);
curCol++;
} else {
success = true;
break;
}
}
if (success == true) {
System.out.println(list);
Scanner scan = new Scanner(System.in);
scan.next();
}
curCol--;
// 斜向左
while ((++curRow) < n & (--curCol) >= 0) {
if (!list.contains(array[curRow][curCol])) {
list.add(array[curRow][curCol]);
} else {
success = true;
break;
}
}
if (success == true) {
System.out.println(list);
Scanner scan = new Scanner(System.in);
scan.next();
}
curRow--;
curCol++;
// 向左
while ((--curCol) >= 0) {
if (!list.contains(array[curRow][curCol])) {
list.add(array[curRow][curCol]);
} else {
success = true;
break;
}
}
if (success == true) {
System.out.println(list);
Scanner scan = new Scanner(System.in);
scan.next();
}
if (curCol < 0)
curCol++;
// 向上
curRow--;
if (!list.contains(array[curRow][curCol])) {
list.add(array[curRow][curCol]);
} else {
success = true;
}
if (success == true) {
System.out.println(list);
Scanner scan = new Scanner(System.in);
scan.next();
}
// 向右
while ((++curCol) < m) {
if (!list.contains(array[curRow][curCol])) {
list.add(array[curRow][curCol]);
} else {
success = true;
break;
}
}
if (success == true) {
System.out.println(list);
Scanner scan = new Scanner(System.in);
scan.next();
}
}
#4
刚刚用命令模式写了一个,
发现比较长,论坛不能添加附件,代码只贴主程序部分
楼上的很简略,按面向过程一步步求解就可以
这个也没什么算法
按一定方式算二维坐标值
将遇到的值加入到list里面
每次加入先判断list里面有没有
没有则加入,有则终止
发现比较长,论坛不能添加附件,代码只贴主程序部分
楼上的很简略,按面向过程一步步求解就可以
这个也没什么算法
按一定方式算二维坐标值
将遇到的值加入到list里面
每次加入先判断list里面有没有
没有则加入,有则终止
public class CommandTest {
public static void main(String[] args) {
int[][] arrays = {
{1,2,3,4,5},
{6,7,8,9,10},
{11,12,13,14,15},
{16,17,18,19,20},
};
DataModel dataModel = new DataModel(arrays, 0, 0);
Command toE = new CommandMoveToE(dataModel);
Command toSW = new CommandMoveToSW(dataModel);
Command toN = new CommandMoveToN(dataModel);
Command toW = new CommandMoveToW(dataModel);
MoveCase moveCase = new MoveCase();
moveCase.addCommand(toE);
moveCase.addCommand(toSW);
moveCase.addCommand(toW);
moveCase.addCommand(toN);
moveCase.execute();
}
}
#5
我补充一点,数组是一维数组
数组:int arr[]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};而且里面的15个元素不定,也有可能是18个元素,那么就是6行3列等
#6
