1. 元素累加

有一个包含n个整数的数组arr,请计算出数组中所有元素之和。(假设中间结果以及最终结果都不会超出32位有符号整型的范围)

        static int SumOfArray(int[] intArr)
        {
            if (intArr.Length == 0 || intArr == null)
            {
                throw new Exception("Input Array can not be empty");
            }

            int result = 0;
            for (int i = 0; i < intArr.Length; i++)
            {
                result += intArr[i];
            }
            return result;
        }
View Code
原文地址:https://www.cnblogs.com/Ligeance/p/3393579.html