现在有一个键值对list 存放着一些计算结果 这些结果的具体数是不确定的,
有一个变量a的值也是不确定的 
我现在想在list中取这个变量a/2左右的值
怎么取?

解决方案 »

  1.   

    list中有14,15,34,56,78,43,88a=50那么我现在就要56这个值
      

  2.   


    List<int> ilist = new List<int>();
                ilist.Add(14);
                ilist.Add(15);
                ilist.Add(34);
                ilist.Add(56);
                ilist.Add(78);
                ilist.Add(43);
                ilist.Add(88);
                ilist.Sort();
                int num = 50;
                int smallint = ilist.FindLast(match => match <= num);
                int maxint = ilist.First(match => match >= num);
                int ivalue = num - smallint >= maxint - num ? maxint : smallint;
    当然还有很多需要处理的异常,这里没有加,只是一个简单的示例
      

  3.   


            static void Main(string[] args)
            {
                List<int> lists = new List<int>();
                int j = 100;
                int[] arr = new int[] { 14, 15, 34, 56, 78, 43, 88 };
                lists.AddRange(arr);
                for (int i = 0; i < lists.Count; i++)
                {
                    lists[i] = lists[i] - j;
                }
                lists.Sort();
                Console.WriteLine((lists[lists.Count-1]+j).ToString());
                Console.ReadLine();
            }