java版菱形矩阵
来源:互联网 发布:淘宝挂起是什么意思 编辑:程序博客网 时间:2024/05/16 23:57
用循环做完了。。 老师要求用递归做。。。 哎 有点思路 写写看
/**
* 1* 2 9 8
* 3 10 13 12 7
* 4 11 6
* 5
*
*
*/
class Test2 {
public static void main(String[] args) {
int n=7;
int x=0,y=0,count=0;
int[][] arr = new int[n][n];
count = arr[x][y=n/2]= 1;
boolean b = true;
while(b){
while(x+1<=n/2){//左上
arr[++x][--y] = ++count;
}
while(y+1<=n/2){//左下
arr[++x][++y] = ++count;
}
while(x-1>=n/2){//右下
arr[--x][++y]=++count;
}
while(y-1>n/2){//右上
arr[--x][--y]=++count;
if(!(arr[x-1][y-1]==0)){ //判断是不是已经填完一圈了
arr[x][--y] =++count;
}
}
if(x==(n/2)&&y==((n/2)+1)){ //最终会循环到最中心
arr[x][--y] = ++count;
b=false;
}
}
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length; j++) {
if(arr[i][j]==0){
System.out.print(" ");
}else{
System.out.print(arr[i][j]+" ");
}
}
System.out.println();
}
}
}
- java版菱形矩阵
- java 菱形
- java菱形
- java菱形
- java菱形
- java菱形
- java菱形
- java打印菱形,空心菱形
- java 打印菱形和空心菱形
- 用java实现打印菱形,空心菱形
- Java打印菱形和空心菱形
- 打印菱形与空心菱形java
- 用java 打印菱形
- java写的菱形
- java打印菱形思路
- 用java打印菱形
- Java打印出菱形
- java 实现空心菱形
- 微信!随时随地,就看这十个微信公众帐号
- Android游戏开发---碰撞检测
- [Latex]PGF and Tikz 中label的使用
- 内存对齐的思考
- Android开发环境搭建
- java版菱形矩阵
- linux ubuntu 玩游戏
- 驱动相关
- checklistBox默认选中多个选项
- VMware 上添加硬盘分区以及删除硬盘
- 找出运行错误的元凶
- Mac 模态窗口关闭事件的处理
- CvSeq 介绍
- BASE64 加密与解密的使用