zoukankan      html  css  js  c++  java
  • 一道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

  • 相关阅读:
    BZOJ3098 Hash Killer II 【概率】
    BZOJ4010 [HNOI2015]菜肴制作 【拓扑排序 + 贪心】
    洛谷P4364 [九省联考2018]IIIDX 【线段树】
    洛谷P4363 [九省联考2018]一双木棋chess 【状压dp】
    洛谷P2664 树上游戏 【点分治 + 差分】
    BZOJ1189 [HNOI2007]紧急疏散evacuate 【二分 + 网络流】
    BZOJ1068 [SCOI2007]压缩 【区间dp】
    BZOJ4033 [HAOI2015]树上染色 【树形dp】
    BZOJ4819 [Sdoi2017]新生舞会 【01分数规划 + 费用流】
    排序
  • 原文地址:https://www.cnblogs.com/javawebsoa/p/2981243.html
Copyright © 2011-2022 走看看