复习recursion

1. 如果这样数列赋值在recursion之前,则:

private void lop(int x, int size, double[] jerry){
		if(x==size-1){
			jerry[x] = -1;
			return;
		}
		if(jerry[x+1]==0){
			jerry[x+1] = jerry[x]- 1;  //if so, then jerry={1,1,3.14}
			lop(x+1, size, jerry);
		}
		//jerry[x] = jerry[x+1]+ 1;
	}

2. 如果赋值在recursion最后,则:(本意)

private void lop(int x, int size, double[] jerry){
		if(x==size-1){
			jerry[x] = -1;
			return;
		}
		if(jerry[x+1]==0){
			//jerry[x+1] = jerry[x]- 1;  
			lop(x+1, size, jerry);
		}
		jerry[x] = jerry[x+1]+ 1;  //if so, then jerry={5.14,4.14,3.14}
	}

3. 感觉recursion很容易漏掉某些值。所以要多次对instance调用。

/* May 10,2014
 * to understand seamcarver by msi-pl
 * learn how recur in for loop
 */

public class recur{
	public recur(){
		
	}
	private void lop(int x, int y, int size, double[][] jing){
		double feed = 3.14;
		if(y==size-1){
			if(x==0)
				jing[x][y] = feed;
			if(x==1)
				jing[x][y] = feed*10;
			if(x==2)
				jing[x][y] = feed*100;	
			return;
		}
		for(int m = 0; m <size; m++){
			int px = (x+m)%3;
			//if(px >= size)
				//px = 0;
				//continue;
			if(jing[px][y+1]==0){
				lop(px, y+1, size, jing);
			}
		}
		jing[x][y] = jing[x][y+1]+ 1;
	}
	
	public void sh(int dx){
		recur jerry = new recur();
		double[][] tony = new double[dx][dx];
		jerry.lop(0, 0, dx, tony);
		jerry.lop(1, 0, dx, tony);  // need to do so for jerry[1][0];
		jerry.lop(2, 0, dx, tony);  //need to do so for jerry[2][0];
		
		for(int i = 0; i<dx; i++){
			for(int j=0; j<dx; j++){
				StdOut.println(tony[i][j]);
			}
		}
	}
	public static void main(String[] args){
		recur shishi = new recur();
		shishi.sh(3);
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值