📜  执行n步以[count] [digit]格式转换数字的每个数字

📅  最后修改于: 2021-05-04 07:15:18             🧑  作者: Mango

给定数字num作为字符串和数字N。任务是编写一个程序,该程序在执行N个步骤后将给定的数字num转换为另一个数字。在每一步骤中,num的每个数字将以[count] [digit]格式写入新数字,其中count是一个数字以num连续出现的次数。

例子:

方法:将字符串的字符解析为一个数字,并维护该数字的计数,直到找到另一个数字为止。找到不同的数字后,将数字的计数添加到新的字符串,并将数字添加到其中。字符串完全解析后,使用此新字符串再次重复该函数,直到完成n个步骤为止。

下面是上述方法的实现:

C++
// C++ program to convert number
// to the format [count][digit] at every step
#include 
using namespace std;
  
// Function to perform every step
void countDigits(string st, int n)
{
  
    // perform N steps
    if (n > 0) {
        int cnt = 1, i;
        string st2 = "";
  
        // Traverse in the string
        for (i = 1; i < st.length(); i++) {
            if (st[i] == st[i - 1])
                cnt++;
            else {
                st2 += ('0' + cnt);
                st2 += st[i - 1];
                cnt = 1;
            }
        }
  
        // for last digit
        st2 += ('0' + cnt);
        st2 += st[i - 1];
  
        // recur for current string
        countDigits(st2, --n);
    }
  
    else
        cout << st;
}
  
// Driver Code
int main()
{
  
    string num = "123";
    int n = 3;
  
    countDigits(num, n);
  
    return 0;
}


Java
// Java program to convert number 
// to the format [count][digit] at every step
class GFG 
{
  
    // Function to perform every step
    public static void countDigits(String st, int n)
    {
  
        // perform N steps
        if (n > 0) 
        {
            int cnt = 1, i;
            String st2 = "";
  
            // Traverse in the string
            for (i = 1; i < st.length(); i++) 
            {
                if (st.charAt(i) == st.charAt(i - 1))
                    cnt++;
                else
                {
                    st2 += ((char) 0 + (char) cnt);
                    st2 += st.charAt(i - 1);
                    cnt = 1;
                }
            }
  
            // for last digit
            st2 += ((char) 0 + (char) cnt);
            st2 += st.charAt(i - 1);
  
            // recur for current string
            countDigits(st2, --n);
        } 
        else
            System.out.print(st);
    }
  
    // Driver Code
    public static void main(String[] args) 
    {
        String num = "123";
        int n = 3;
        countDigits(num, n);
    }
}
  
// This code is contributed by
// sanjeev2552


Python
# Python program to convert number
# to the format [count][digit] at every step
  
# Function to perform every step
def countDigits(st, n):
  
    # perform N steps
    if (n > 0) :
        cnt = 1
        i = 0
        st2 = ""
        i = 1
          
        # Traverse in the string
        while (i < len(st) ) :
            if (st[i] == st[i - 1]):
                cnt = cnt + 1
            else :
                st2 += chr(48 + cnt)
                st2 += st[i - 1]
                cnt = 1
            i = i + 1
  
        # for last digit
        st2 += chr(48 + cnt)
        st2 += st[i - 1]
  
        # recur for current string
        countDigits(st2, n - 1)
        n = n - 1;
  
    else:
        print(st)
  
# Driver Code
  
num = "123"
n = 3
  
countDigits(num, n)
  
# This code is contributed by Arnab Kundu


C#
// C# program to convert number 
// to the format [count][digit] at every step
using System;
class GFG 
{
  
// Function to perform every step
public static void countDigits(string st, int n)
{
  
    // perform N steps
    if (n > 0) 
    {
        int cnt = 1, i;
        string st2 = "";
  
        // Traverse in the string
        for (i = 1; i < st.Length; i++) 
        {
            if (st[(i)] == st[(i - 1)])
                cnt++;
            else
            {
                st2 += ((char) 0 + (char) cnt);
                st2 += st[(i - 1)];
                cnt = 1;
            }
        }
  
        // for last digit
        st2 += ((char) 0 + (char) cnt);
        st2 += st[(i - 1)];
  
        // recur for current string
        countDigits(st2, --n);
    } 
    else
        Console.Write(st);
}
  
// Driver Code
public static void Main() 
{
    string num = "123";
    int n = 3;
    countDigits(num, n);
}
}
  
// This code is contributed by
// Code_Mech.


输出:
1321123113
想要从精选的最佳视频中学习并解决问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”