求递归算法
用递归算法画出该图形,递归方法的参数为left和right,第一次进入该程序时,left为0,right为宽度减1,即为15。
我想用数组来保存该图形,请哪位大哥指点如何实现
--------x-------
----x-------x---
--x---x---x---x-
-x-x-x-x-x-x-x-x
xxxxxxxxxxxxxxxx
------解决方案-------------------- private int getLayer(float left,float right) {
if ((right == (result[0].length-1)) && (left != (result[0].length-1))) {
right++;
}
int diff = (int)(right - left);
int pow = result.length;
while (diff > 0) {
diff /= 2;
pow--;
}
return pow;
}
private void draw(float left, float right) {
int layer = getLayer(left,right);
if (layer > = (result.length-1)) {
result[layer][0] = 'x ';
result[layer][result[0].length-1] = 'x ';
}
result[layer][Math.round((left+right)/2)] = 'x ';
if ((right - left) > 1) {
draw(left,Math.round((left+right)/2));
draw(Math.round((left+right)/2),right);
}
}