zoukankan      html  css  js  c++  java
  • 字符串编码C#

    给定一个字符串,请你将字符串重新编码,将连续的字符替换成“连续出现的个数+字符”。比如字符串AAAABCCDAA会被编码成4A1B2C1D2A。 

    输入描述:

    每个测试输入包含1个测试用例
    每个测试用例输入只有一行字符串,字符串只包括大写英文字母,长度不超过10000。

    输出描述:

    输出编码后的字符串

    输入例子:

    AAAABCCDAA

    输出例子:

    4A1B2C1D2A

    牛客网页运行代码如下:

    using System;
    public class Program{
        public static void Main(){
             string input = System.Console.ReadLine();
                int inputLength = input.Length;
                int keyCount = 0;
                char keyValue;
                char[] c = new char[inputLength];
                for (int i = 0; i < inputLength; i++)
                {
                    c[i] = Convert.ToChar(input.Substring(i, 1));
                }
                for (int i = 0; i < inputLength; )
                {
                    keyValue = c[i];
                    while (i != inputLength && keyValue == c[i])
                    {
                        i++;
                        keyCount++;
                    }
                    Console.Write("{0}{1}", keyCount, keyValue);
                    keyCount = 0;
                }
                Console.ReadKey();
        }
    }

    VS中的代码如下:

    using System;
    
    namespace 字符串编码
    {
        class Program
        {
            static void Main(string[] args)
            {
                string input = System.Console.ReadLine();
                int inputLength = input.Length;
                int keyCount = 0;
                char keyValue;
                char[] c = new char[inputLength];
                for (int i = 0; i < inputLength; i++)
                {
                    c[i] = Convert.ToChar(input.Substring(i, 1));
                }
                for (int i = 0; i < inputLength; )
                {
                    keyValue = c[i];
                    while (i != inputLength && keyValue == c[i])
                    {
                        i++;
                        keyCount++;
                    }
                    Console.Write("{0}{1}", keyCount, keyValue);
                    keyCount = 0;
                }
                Console.ReadKey();
            }
        }
    }

     欢迎交流。

  • 相关阅读:
    【转】 cin、cin.get()、cin.getline()、getline()、gets()等函数的用法
    HDU How many prime numbers
    《大学ACM的总结 》(转载)
    POJ 最小公倍数
    HDU 开门人和关门人
    HDU shǎ崽 OrOrOrOrz
    HDU Saving HDU 2111
    HDU 1106 排序
    strtok函数()
    HDU 2187汶川地震
  • 原文地址:https://www.cnblogs.com/BrokenIce/p/6531070.html
Copyright © 2011-2022 走看看