遞歸求和數組

遞歸求和數組

/** * 遞歸求和數組 * Created by xueping.you on 15-8-5. */
public class RecursionSum {
    private final static Logger logger = LoggerFactory.getLogger(RecursionSum.class);

    public static int getSum(int []arry , int len){
        return len==0?0 : getSum(arry, len-1)+arry[len-1];
    }

    public static void main(String []args){
        logger.info("sum result={}" ,getSum(new int[]{1,2,3,4,5,6} , 6));
    }
}

result:
22:47:32.738 [main] INFO c.qyou.data.arithmetic.RecursionSum – sum result=21

点赞