• 杨辉三角与数组递归累加


    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);        
        }
        
        
        
    
    }
  • 相关阅读:
    谈谈架构层级的“开闭原则”
    将MySQL数据库中的表结构导入excel 或word
    淘宝网-软件质量属性分析
    架构漫谈阅读有感
    机器学习-分类算法之决策树、随机森林
    机器学习-分类算法之逻辑回归
    机器学习-朴素贝叶斯算法
    机器学习-分类算法之k-近邻
    机器学习-模型选择
    机器学习-scikit-learn数据集
  • 原文地址:https://www.cnblogs.com/aigeileshei/p/5549340.html
Copyright © 2020-2023  润新知