• 1,2,3,5,7,8,10,11,12,13,14,15,16,21,22 》1~3,5,7~8,10~16,21~22


    为了省略空间以及符合操作手手工填写纸张的视觉,决定把料架随行卡上的卷号进行归类整理,如

    A-3,A-4,A-5,A-8形成A-3~5,8等。

    话不多说了,用了几个辅助的List<int>完成,代码如下:

     /// <summary>
    /// 类似的1,2,3,5,7,8,10,11,12,13,14,15,16,21,22转换成1~3,5,7~8,10~16,21~22
    /// </summary>
    /// <param name="strInput">传入字符串,如1,2,3,5,7,8,10,11,12,13,14,15,16,21,22</param>
    /// <returns>生成的新字符串,如1~3,5,7~8,10~16,21~22</returns>
    private string makeSimpleString(string strInput)
    {
        string strOutput = "";
        //把为整数的放到il中
        string[] ss = strInput.Split(',');
        List<int> il = new List<int>();
        for (int i = 0; i < ss.Length; ++i)
        {
            try
            {
                int k = Convert.ToInt32(ss[i]);
                il.Add(k);
            }
            catch
            {
                if (strOutput == "")
                    strOutput = ss[i];
                else
                    strOutput += "," + ss[i];
            }
        }

        
        if (il.Count > 0)
        {
            //把il中的连续的数各放到不同的List<int>中
            il.Sort();
            List<List<int>> ill = new List<List<int>>();
            int prev = il[0];
            List<int> lTemp = new List<int>();
            lTemp.Add(prev);
            ill.Add(lTemp);
            for (int i = 1; i < il.Count; ++i)
            {
                if (il[i] - prev == 1)
                {
                    ill[ill.Count - 1].Add(il[i]);
                }
                else
                {
                    lTemp = new List<int>();
                    lTemp.Add(il[i]);
                    ill.Add(lTemp);
                }
                prev = il[i];
            }

            //生成结果字符串
            for (int i = 0; i < ill.Count; ++i)
            {
                if (ill[i].Count == 1)
                {
                    if (strOutput == "")
                        strOutput = ill[i][0].ToString();
                    else
                        strOutput += "," + ill[i][0].ToString();
                }
                else
                {
                    if (strOutput == "")
                        strOutput = ill[i][0].ToString() + "~" + ill[i][ill[i].Count - 1].ToString();
                    else
                        strOutput += "," + ill[i][0].ToString() + "~" + ill[i][ill[i].Count - 1].ToString();
                }
            }
        }

        return strOutput;
    }

  • 相关阅读:
    android -------- Data Binding的使用(二)
    牛客网-《剑指offer》-数值的整数次方[快速幂运算]
    牛客网-《剑指offer》-二进制中1的个数
    牛客网-《剑指offer》-矩形覆盖
    牛客网-《剑指offer》-变态跳台阶
    牛客网-《剑指offer》-跳台阶
    牛客网-《剑指offer》-斐波那契数列
    牛客网-《剑指offer》-旋转数组的最小数
    牛客网-《剑指offer》-用两个栈实现队列
    牛客网-《剑指offer》-重建二叉树
  • 原文地址:https://www.cnblogs.com/tianfu/p/1691562.html
Copyright © 2020-2023  润新知