📜  exponet - Java 代码示例

📅  最后修改于: 2022-03-11 14:52:11.361000             🧑  作者: Mango

代码示例2
public class Recursion {
    public static void main(String[] args) {
        int[] arr = {23,21,321,312,31,23,12,312,312,312,3,18,200000,1};
        int arrSum = 0;
        arrSum= sigma(arr,arr.length-1);

        int sum;
        sum = sigma(arr.length);
        System.out.println("Sum of Array Elements:");
        System.out.println(arrSum+"\n");
        System.out.println("sigma n ==> (Arr.length) \n"+arr.length+"\n"+ sum);
    }


    public static int sigma(int n){
        if(n<=0)
            return n;

        return n + sigma(n-1);


    }

    public static int sigma(int[] arr, int l){

        if(l == 0)
            return arr[l];

        return arr[(l)] + sigma(arr,l-1) ;
    }
}