一道C#面试题

C#面试题统计"0"字符数量,并将统计数字插入到字符中。要求:
输入:
rnbakabnr/000000000/0c00000c0/p0p0p0p0p/000000000/000000000/P0P0P0P0P/0C00000C0/000000000/RNBAKABNR
输出:
rnbakabnr/9/1c5c1/p1p1p1p1p/9/9/P1P1P1P1P/1C5C1/9/RNBAKABNR

 

答案:

 

//递归算法

  private string ProssStr(string tempStr)
        {
            string tempAll = "";
            int j0 = tempStr.IndexOf('0');
            if (j0 > -1)
            {
                int j1 = 0;
                for (int i = 0; i < tempStr.Length - j0; i++)
                {
                    string k2 = tempStr.Substring(j0 + i, 1);
                    if (k2 == "0")
                    {
                        j1 = j1 + 1;
                    }
                    else
                    {
                        break;
                    }
                }
                string tempStr1 = tempStr.Remove(j0, j1).Insert(j0, j1.ToString());
                tempAll = ProssStr(tempStr1);
            }
            else
            {
                tempAll = tempStr;
            }
            return tempAll;
        }

调用:

 string tempStrAll = "";
            string s = "rnbakabnr/000000000/0c00000c0/p0p0p0p0p/000000000/000000000/P0P0P0P0P/0C00000C0/000000000/RNBAKABNR";//输入
            tempStrAll = ProssStr(s);//执行算法

输出到变量tempStrAll:

rnbakabnr/9/1c5c1/p1p1p1p1p/9/9/P1P1P1P1P/1C5C1/9/RNBAKABNR

 

 

posted @ 2013-03-25 18:51  javawebsoa  Views(227)  Comments(0Edit  收藏  举报