文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>趣味编程(一)

趣味编程(一)

时间:2010-10-29  来源:hans.hu

public T MaxElement<T, TCompare>(this IEnumerable<T> collection, Func<T, TCompare> func) where TCompare : IComparable<TCompare>
{
    T maxItem = default(T);
    TCompare maxValue = default(TCompare);
    foreach (var item in collection) {
        TCompare temp = func(item);
        if (maxItem == null || temp.CompareTo(maxValue) > 0) {
            maxValue = temp;
            maxItem = item;
        }
    }
    return maxItem;
}
string findStr = "Beijing";
var rm = findStr.ToCharArray().GroupBy(c => c).Select(g => new { g.Key, Count = g.Count() }).MaxElement(r => r.Count);

2、需求:生成不重复的随机数并排序。

public IList<int> GetRandoms(int min, int max, int length)
{
    IList<int> rlist = new List<int>();
    int index = 1;
    int seed = 0;
    while (index < length) {
        var random = new Random(seed++).Next(min, max);
        if (!rlist.Contains(random))
            rlist.Add(random);
        index = rlist.Count();
    }

    return rlist;
}
List<int> rlist = GetRandoms(10, 99, 20).ToList();
rlist.Sort((x, y) => Math.Sign(x - y));
相关阅读 更多 +
排行榜 更多 +
西安交大通

西安交大通

生活实用 下载
长江云通

长江云通

生活实用 下载
translatez

translatez

生活实用 下载