标签:
优化前:
/// <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;
}
多思考。
标签:
原文地址:http://www.cnblogs.com/abelverycool/p/5521635.html