`

递归算法

    博客分类:
  • java
 
阅读更多
                1 
              1 2 1 
            1 2 3 2 1 
          1 2 3 4 3 2 1 
        1 2 3 4 5 4 3 2 1 
      1 2 3 4 5 6 5 4 3 2 1 
    1 2 3 4 5 6 7 6 5 4 3 2 1 
  1 2 3 4 5 6 7 8 7 6 5 4 3 2 1 
1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1 

//递归算法
public class Recursion {

	public static void main(String[] args) {
		
		//总行数
		int sumRow=9;
		
		for(int i=1;i<=sumRow;i++){
			
			for(int j=sumRow;j>i;j--){
				
				//打印空格
				System.out.print("  ");
			}
			count(1,i);
			
			//回车换行执行下一次
			System.out.println();
		}
	}
	
	//打印一行
	public static void count(int beginIndex,int endIndex){
		
		if(beginIndex<endIndex){
			
			System.out.print(beginIndex+" ");
			//再次调用自己
			count(beginIndex+1,endIndex);
		}
		System.out.print(beginIndex+" ");
	}
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics