zoukankan      html  css  js  c++  java
  • 杨辉三角与数组递归累加

    package test.面试题;
    
    public class Test7 {
        public static void main(String[] args){
            /**
             1
             1    1
             1    2    1
             1    3    3    1
             1    4    6    4    1
             1    5    10    10    5    1
             ...    
             
                                    规律:
                                        每一行的最后一列和第一列都为1
                                        从第三行开始每一行的第二位到倒数第二位是上一列的前一列和本列的和
             */
            
            /*第一种方法
            int[][] arr=new int[6][6];
            for(int i=0;i<arr.length;i++){
                arr[i][0]=1;
                arr[i][i]=1;
                for(int j=1;j<i;j++){
                    if(i>1){
                        arr[i][j]=arr[i-1][j-1]+arr[i-1][j];
                    }
                    
                }        
            }
            
        for(int i=0;i<arr.length;i++){
            for(int j=0;j<i;j++){
                System.out.print(arr[i][j]+"	");
            }
            System.out.println();
        }*/                
        
        }
    
    }
    package test.面试题;
    
    public class Test8 {
        /**
         不使用循环达到数组求和
         [java] view plain copy
    print?
    
        package hello;  
          
        public class TestA {  
            public static int sum( int[] numbers )  
            {  
               int total = 0;  
               for ( int n : numbers )  
                  total += n;                   
               return total;  
            }  
              
            public static int sum2( int... numbers )  
            {  
               return sum2Helper(0, 0, numbers);  
            }  
              
            private static int sum2Helper( int total, int i, int... numbers)  
            {  
               return i == numbers.length ? total :  
                   sum2Helper(total + numbers[i], i + 1, numbers);  
            }  
              
            public static int sum3( int... numbers )  
            {  
               return sum3Helper(0, numbers);  
            }  
              
            private static int sum3Helper( int i, int... numbers)  
            {  
               return i == numbers.length ? 0 :  
                   numbers[i] + sum3Helper(i + 1, numbers);  
            }  
              
            public static void main( String args[] )  
            {  
                System.out.println(sum(1, 2, 3, 4));  
                System.out.println(sum2(1, 2, 3, 4));  
                System.out.println(sum3(1, 2, 3, 4));  
            }  
        }  
          
        //运行结果  
        //10  
        //10  
        //10  
    
    代码说明
    
        sum函数使用普通的for each循环对数组求和。
        sum([1, 2, 3, 4]) = 0 + 1 + 2 + 3 + 4
        sum2函数不使用循环而是用递归(左卷起)方式对数组求和。
        sum2([1, 2, 3, 4]) = (((0 + 1) + 2) + 3) + 4
        sum3函数不使用循环而是用递归(右卷起)方式对数组求和。
        sum3([1, 2, 3, 4]) = 1 + (2 + (3 + (4 + 0)))
         
         */
        
        public static void main(String[] args){
            int[] a={1,2,3,4};
            System.out.println(sum(1,a));
        }
        public static int sum(int i,int[] arr){    
                return arr.length<=i?0:arr[i]+sum(i+1,arr);        
        }
        
        
        
    
    }
  • 相关阅读:
    统计数据持久化
    缓存层的实现
    C++语法疑点
    为什么需要定义虚的析构函数?
    C++ shared_ptr deleter的实现
    条件变量
    ubuntu  输入时弹出剪切板候选项
    leetcode Bitwise AND of Numbers Range
    C/C++ 字符串 null terminal
    C++ inline weak symbol and so on
  • 原文地址:https://www.cnblogs.com/aigeileshei/p/5549340.html
Copyright © 2011-2022 走看看