一算法,敬请指导 是要打出一个数字排列图形样子大概是这样的: 1 2 3 4 5 14 15 16 17 6 13 20 19 18 7 12 11 10 9 8 不只是4行5列,要对任意的都适用,怎么打啊 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 class HelixMatrix { int[][] myMatrix = new int[20][20]; static int H = 5; //这个是5列 static int L = 4; //这个是4行。这2个可以定义几行几列 static int count = 0; //填充数字记数 int hh = H; //矩阵实际大小 int ll = L; int stepX = hh; //初始步距 int stepY = ll - 1; int x = 0; //初始坐标 int y = 0; void fillFromLeftToRight(int step) { //从左向右填写 for (int i = 0; i < step; i++) { if (myMatrix[y][x] == 0) { myMatrix[y][x] = ++count; } else { x++; myMatrix[y][x] = ++count; } } } void fillFromUpToDown(int step) { //从上向下填写 for (int i = 0; i < step; i++) { y++; myMatrix[y][x] = ++count; } } void fillFromRightToLeft(int step) { //从右向左填写 for (int i = 0; i < step; i++) { x--; myMatrix[y][x] = ++count; } } void fillFromDownToUp(int step) { //从下向上填写 for (int i = 0; i < step; i++) { y--; myMatrix[y][x] = ++count; } } public void make() { //生成矩阵:从左到右、从上到下、从右到左、 do { //从下到上,每一循环填一圈,很好理解吧。:) if (count != H * L) { fillFromLeftToRight(stepX--); } if (count != H * L) { fillFromUpToDown(stepY--); } if (count != H * L) { fillFromRightToLeft(stepX--); } if (count != H * L) { fillFromDownToUp(stepY--); } } while (count != H * L); } public void print() { //打印出来,要注意排版哟!J if语句控制补空格 for (int i = 0; i < L; i++) { for (int j = 0; j < H; j++) { if (myMatrix[i][j] < 10) { System.out.print(" " + myMatrix[i][j]); } else if (myMatrix[i][j] >= 100) { System.out.print(" " + myMatrix[i][j]); } else { System.out.print(" " + myMatrix[i][j]); } } System.out.println(); } }}public class PrintHelixMatrix { public static void main(String[] args) { HelixMatrix aa = new HelixMatrix(); aa.make(); aa.print(); }} 如何通过httpclient访问基于windows域服务器摘要身份证认证服务器提供的url java源代码生成可运行JAR文件 错误了 JAVA输入输出问题 rownum和updateRow()之间的问题 学好java的方法 各位大哥来帮小弟看看这个需求吧 java如何打包及制作安装文件? 如何把图片从服务器端读出后,存在byte[]中 为什么说SwingPropertyChangeSupport这个类不是线程安全的呢? 如何使用JDBC FOR ORACLE 求助, 一个简单一元硬币问题! 请教,为什么编译有错误的问题,还可以运行呢?
static int H = 5; //这个是5列
static int L = 4; //这个是4行。这2个可以定义几行几列
static int count = 0; //填充数字记数
int hh = H; //矩阵实际大小
int ll = L;
int stepX = hh; //初始步距
int stepY = ll - 1;
int x = 0; //初始坐标
int y = 0; void fillFromLeftToRight(int step) {
//从左向右填写
for (int i = 0; i < step; i++) {
if (myMatrix[y][x] == 0) {
myMatrix[y][x] = ++count;
} else {
x++;
myMatrix[y][x] = ++count;
}
}
} void fillFromUpToDown(int step) {
//从上向下填写
for (int i = 0; i < step; i++) {
y++;
myMatrix[y][x] = ++count;
}
} void fillFromRightToLeft(int step) {
//从右向左填写
for (int i = 0; i < step; i++) {
x--;
myMatrix[y][x] = ++count;
}
} void fillFromDownToUp(int step) {
//从下向上填写
for (int i = 0; i < step; i++) {
y--;
myMatrix[y][x] = ++count;
}
} public void make() {
//生成矩阵:从左到右、从上到下、从右到左、
do {
//从下到上,每一循环填一圈,很好理解吧。:)
if (count != H * L) {
fillFromLeftToRight(stepX--);
}
if (count != H * L) {
fillFromUpToDown(stepY--);
}
if (count != H * L) {
fillFromRightToLeft(stepX--);
}
if (count != H * L) {
fillFromDownToUp(stepY--);
}
} while (count != H * L);
} public void print() {
//打印出来,要注意排版哟!J if语句控制补空格
for (int i = 0; i < L; i++) {
for (int j = 0; j < H; j++) {
if (myMatrix[i][j] < 10) {
System.out.print(" " + myMatrix[i][j]);
} else if (myMatrix[i][j] >= 100) {
System.out.print(" " + myMatrix[i][j]);
} else {
System.out.print(" " + myMatrix[i][j]);
}
}
System.out.println();
}
}
}
public class PrintHelixMatrix { public static void main(String[] args) {
HelixMatrix aa = new HelixMatrix();
aa.make();
aa.print();
}
}