问题:给定逗号分隔的数字字符串,想要找出数字的最高位数.例如:考虑一串数字"123,345,555",输出为5,因为5是123,345,555中的最高平均值.
这是我的程序..在C#
int Max_Avg(string number_list)
{
int i;
Int32 sum;
Int32 max_avg = 0;
int limit = 0;
string[] num = number_list.Split(',');
limit = num.Length;
while (limit-- > 0)
{
i=0;
sum = 0;
while (i < num[limit].Length)
{
sum += Convert.ToInt32(num[limit].Substring(i, 1));
i++;
}
int tmp=sum / num[limit].Length;
if (tmp > max_avg)
{
max_avg = tmp;
}
}
return max_avg;
}
Run Code Online (Sandbox Code Playgroud)
如果有任何可以优化其性能或建议更优化的方法来加快它.....
(针对可维护性/代码而非原始性能进行了优化)
return (int) number_list.Split(',')
.Select(term => term.Average(c => (int) (c - '0'))).Max();
Run Code Online (Sandbox Code Playgroud)