码迷,mamicode.com
首页 > 其他好文 > 详细

int类型各位数累加

时间:2016-05-23 22:38:02      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:

 优化前:

        /// <summary>计算int类型各位数累加之和(1)</summary>
        static int Calc1(int i)
        {
            int result = 0;
            var array = i.ToString().ToCharArray();
            foreach (var item in array)
            {
                result += Convert.ToInt32(item);
            }
            return result;
        }

优化后:

        /// <summary>计算int类型各位数累加之和(2)</summary>
        static int Calc2(int i)
        {
            int result = 0;
            while (i > 0)
            {
                result += i % 10;
                i /= 10;
            }
            return result;
        }

多思考。

int类型各位数累加

标签:

原文地址:http://www.cnblogs.com/abelverycool/p/5521635.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!