1 16 15 14 13
2 17 24 23 12
3 18 25 22 11
4 19 20 21 10
5 6 7 8 9
一个这样的二维数组如何打印输出?
2 17 24 23 12
3 18 25 22 11
4 19 20 21 10
5 6 7 8 9
一个这样的二维数组如何打印输出?
解决方案 »
- java swing界面怎么根据逻辑更改
- 使用UltraEdit编译java程序的问题
- java 胖客户端设计问题
- 请问:j2se,j2me,j2ee各是什么意思呀,有什么区别呀
- 我想在body的onload事件中写一个function,让页面上所有的表格里的数据都变成可编辑状态,怎么做啊??
- 请教一个基础问题,关于堆和栈,以及内存回收的相关知识,请高手进来帮忙,谢谢!平时不关心内存的人也进来学习一下吧。
- 谁能使用JAVA翻译下列C代码?
- 给大家分享点资料以及学习java的思路
- 关于constructor的问题
- 如何在JAVA中得到一个CHAR型变量的ASC值呀?
- 会RCP发布程序的进来帮我小弟看一下咯,谢谢啊
- 如何设置树状结构的外观?
你可以自己改需要打印多少class HelixMatrix { int[][] myMatrix = new int[20][20];
static int H = 16;
static int L = 12;
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();
}
}
final int HEIGHT = 5;
final int WIDTH = 5;
int[][] arr = new int[HEIGHT][WIDTH]; int row = 0; // 控制数组的行
int col = 0; // 控制数组的列
int cycles = 1; // 当前写入的圈数,初始化为最外层第一圈
// 写入数据的方向
// 1 - down; 2 - right; 3 - up; 4 - left
int direct = 1;
for (int num = 1; num <= HEIGHT * WIDTH; num++) {
if (row == HEIGHT - cycles && col == cycles - 1) {
direct = 2;
}
if (row == HEIGHT - cycles && col == WIDTH - cycles) {
direct = 3;
}
if (row == cycles - 1 && col == WIDTH - cycles) {
direct = 4;
}
if (row == cycles - 1 && col == cycles) {
direct = 1;
cycles++; // 当方向又回到 1 时,说明一圈已经走完,继续下一圈
}
arr[row][col] = num;
// 当方向为向下写时,行增加
row = direct == 1 ? row + 1 : row;
// 当方向为向上写时,行减少
row = direct == 3 ? row - 1 : row;
// 当方向为向右写时,列增加
col = direct == 2 ? col + 1 : col;
// 当方向为向左写时,列减少
col = direct == 4 ? col - 1 : col;
} // 输出数组
for (int[] ar : arr) {
for (int a : ar) {
System.out.printf("%3d", a);
}
System.out.println();
}
}
public static void main (String[] args) {
final int a=5, b=5; //定义矩阵
int n=1; //和打印排版有关
while (n<a*b) n*=10;
n=n*n/10;
int[][] arr = new int[a][b];
int x=0,y=0,s=1;
arr[x][y] = s;
while (s<a*b) { //以下四个循环换下顺序可变成顺时针
while ( x+1<a && arr[x+1][y]==0 ) {
x++; s++; arr[x][y]=s; //down
}
while ( y+1<b && arr[x][y+1]==0 ) {
y++; s++; arr[x][y]=s; //right
}
while ( x-1>=0 && arr[x-1][y]==0 ) {
x--; s++; arr[x][y]=s; //up
}
while ( y-1>=0 && arr[x][y-1]==0 ) {
y--; s++; arr[x][y]=s; //left
}
}
for (int i = 0; i < a; i++) {
for (int j = 0; j < b; j++) {
int m=arr[i][j];
while ( m<n ){ //打印排版有关
System.out.print (" "); m*=10;
} System.out.print (arr[i][j]);
} System.out.println ("");
}
}
}
调试运行正确。#include<stdio.h>
#include<string.h>
#include<conio.h>
#define N 6
main()
{
int str[N][N]={0};
int i,j,a,b,up,down,left,right;
clrscr();
str[0][0]=1;
for(i=0,j=0;i<N,j<N;)
{
if(i-1<0) up=1;
else up=str[i-1][j];
if(i+1>=N) down=1;
else down=str[i+1][j];
if(j-1<0) left=1;
else left=str[i][j-1];
if(j+1>=N) right=1;
else right=str[i][j+1]; if(up!=0&&down!=0&&left!=0&&right!=0) break;
else
if((up!=0&&down==0&&left!=0&&right!=0)||(up!=0&&down==0&&left!=0&&right==0))
{a=i+1; b=j;}
else
if((up==0&&down!=0&&left!=0&&right!=0)||(up==0&&down!=0&&left==0&&right!=0))
{a=i-1; b=j;}
else
if((up!=0&&down!=0&&left==0&&right!=0)||(up!=0&&down==0&&left==0&&right!=0))
{a=i; b=j-1;}
else
if((up!=0&&down!=0&&left!=0&&right==0)||(up==0&&down!=0&&left!=0&&right==0))
{a=i; b=j+1;}
str[a][b]=str[i][j]+1;
i=a;
j=b;
}
for(i=0;i<N;i++)
for(j=0;j<N;j++)
{
printf("%4d",str[i][j]);
if(j==N-1)
printf("\n");
}
}