读书人

请批改一下代码统计最大值

发布时间: 2012-06-20 20:37:21 作者: rapoo

请修改一下代码,统计最大值


如何统计 1 连续出现的最大值 (答案是4)
如何统计 空白连续出现的最大值 (答案是5)

C# code
for (int r = Grid.Rows.Fixed; r < Grid.Rows.Count; r++)            {                if (Grid[r, 1] != null)                {                    initialvalue = Convert.ToInt32(Grid[r, 5]);                    if (initialvalue == 0)                    {                        one_consecutive += 1;                       }                }                else                {                    if (one_consecutive > max_one_consecutive)                    {                        max_one_consecutive = one_consecutive;                        one_consecutive = 0;                    }                }            }


[解决办法]
不知道你grid是啥,所以写了一个通用的例子,你看懂了自己改

C# code
    public class test        {            int? maxCount=null;            int CcurCount = 0;            bool _countState=true;            bool CountState {                get                {                    return _countState;                }                set {                    _countState = value;                    if (!_countState)                    {                        if (CcurCount > (maxCount ?? 0))                            maxCount = CcurCount;                        CcurCount = 0;                    }                                }            }            public int getMaxCount<T>(IEnumerable<T> list, Func<T, bool> func)            {                if (list==null)                {                    return 0;                }                maxCount = null;                foreach (T item in list)                {                    if (func(item))                    {                        CcurCount += 1;                    }                    else                    {                        CountState = false;                    }                                    }                CountState = false;                return maxCount ?? 0;            }                       }
[解决办法]
试试这个
C# code
int one = 0;int zero = 0;bool flag = true;int tc = 0;Action action = () => { if (flag) { if (tc > one) { one = tc; } } else { if (tc > zero) { zero = tc; } } };for (int i = 0; i < Grid.Rows.Count; i++){    if ((Grid[0, i] != null) != flag)    {        action();        tc = 0;        flag = !flag;    }    ++tc;}action();
[解决办法]
C# code
            int blankconsecutive = 0;            int maxblankconsecutive = 0;            int zeroconsecutive = 0;            int maxzeroconsecutive = 0;            for (int r = 1; r <= 25; r++)            {                flex[r, 0] = r;                flex[26, 0] = "///////////////////////";                flex[26, 1] = "///";                if (flex[r, 1] == null)                {                    blankconsecutive += 1;                }                else                {                    if (Convert.ToInt32(flex[r, 1]) == 0)                    {                        if (blankconsecutive > maxblankconsecutive)                        {                            maxblankconsecutive = blankconsecutive;                        }                    }                    blankconsecutive = 0;                }                flex.SetData(29, 1, maxblankconsecutive);                           if (flex[r,1] != null)                {                    zeroconsecutive += 1;                }                else                {                    if (flex[r,1] == null)                    {                        if (zeroconsecutive > maxzeroconsecutive)                        {                            maxzeroconsecutive = zeroconsecutive;                        }                    }                    zeroconsecutive = 0;                }                flex.SetData(28, 1, maxzeroconsecutive);            } 

读书人网 >C#

热点推荐