用list 接 數值,再講數值轉成 整形數組,排序、再將相鄰的數用範圍形式表示、

 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ui

List<int> list1=new List<int>();
                   List<int> list2=new List<int>();
                  
                    foreach (System.Windows.Forms.Control c in panel5.Controls)
                    {
                        if (c is CheckBox)
                        {
                            if (((CheckBox)c).Checked == true)
                            {
                                list1.Add(int.Parse(((CheckBox)c).Text));
                              
                            }
                            else
                            {

                                list2.Add(int.Parse(((CheckBox)c).Text));
                               
                            }
                        }
                    }
                    int[] arr = InsertionSort(list1.ToArray());
                    int[] arrhold = InsertionSort(list2.ToArray());
                    CPYC.normal = zhengli(arr);
                   CPYC.hold = zhengli(arrhold);orm

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
排序

   /// <summary>
        /// 插入排序法
        /// </summary>
        /// <param name="list"></param>
        public static int[] InsertionSort(int[] list)
        {
            for (int i = 1; i < list.Length; i++)
            {
                int t = list[i];
                int j = i;
                while ((j > 0) && (list[j - 1] > t))
                {
                    list[j] = list[j - 1];
                    --j;
                }
                list[j] = t;
            }
            return list;
        }string

/// 整理 將相鄰的數值用範圍的形式表示  {1,2,3}轉成{1-3}
io

public  string  zhengli (int[] arry)
        {
          
            string stringbuild="";
            int start, end;
            for (int i = 0; i < arry.Length; i++)
            {
                start = arry[i];

                while (i < arry.Length - 1 && arry[i] + 1 == arry[i + 1])
                    i++;

                end = arry[i];

                if (start == end)
                {
                    stringbuild += start.ToString()+",";
                }
                else
                {
                    stringbuild += start + " - " + end+",";
                }
               
            }
            return stringbuild;
        }foreach

相關文章
相關標籤/搜索