写个算法:打印如下形式的数字
9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 9 9 8 7 7 7 7 7 7 7 7 7 7 7 7 7 8 9 9 8 7 6 6 6 6 6 6 6 6 6 6 6 7 8 9 9 8 7 6 5 5 5 5 5 5 5 5 5 6 7 8 9 9 8 7 6 5 4 4 4 4 4 4 4 5 6 7 8 9 9 8 7 6 5 4 3 3 3 3 3 4 5 6 7 8 9 9 8 7 6 5 4 3 2 2 2 3 4 5 6 7 8 9 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 9 8 7 6 5 4 3 2 2 2 3 4 5 6 7 8 9 9 8 7 6 5 4 3 3 3 3 3 4 5 6 7 8 9 9 8 7 6 5 4 4 4 4 4 4 4 5 6 7 8 9 9 8 7 6 5 5 5 5 5 5 5 5 5 6 7 8 9 9 8 7 6 6 6 6 6 6 6 6 6 6 6 7 8 9 9 8 7 7 7 7 7 7 7 7 7 7 7 7 7 8 9 9 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9
下面是递归实现:
public class DiGuiHuiXuan {
/**
* 本递归算法得思路是:把二维数组看成回行圈,赋值的时候先赋值二维数组的最外围,
* 有外而内一圈一圈的递归赋值
*注意根据数组下标从0开始的特点,每一圈的子二维数组的maxRow和minRow
* maxColumn 和minColumn有如下数学关系
* maxRow + minRow = array.length -1
* maxColumn + minColumn = array.length-1
* @param array
* @param n 回旋数组开始指定的数,或者说是递归调用中子二维数组的最外圈的值
* @param m 记录每一圈的二维子数组的最大行和最大列
*/
public static void huiXuan(int[][] array, int n, int m) {
//递归出口
if (1 == n) {
//取数组的中心位置
int center = array.length >> 1;
array[center][center] = 1;
return;
}
//最下面的行
int maxRow = m;
//最右边的列
int maxColums = m;
//最上面的行
int minRow = array.length - 1 - maxRow;
//最左边的列
int minColums = array.length - 1 - maxColums;
//(子)二维数组的最上面一行
for (int i = minColums; i
关注
打赏
热门博文
- Flutter之Widget构建过程详解
- Flutter之Widget 更新机制原理浅析
- 从源码角度分析android事件分发处理机制
- android事件拦截处理机制详解
- android 获取IP地址
- android 获取生肖和星座
- Android TextView setCompoundDrawables设置图片不显示解决方案
- Android Stuido集成LeakCanary编译失败TransformException、transformClassesWithDexBuilderForDebug
- Flutter实战之go_router路由组件入门指南
- HarmonyOS鸿蒙学习笔记(9)Navigator组件实现页面路由跳转