📌  相关文章
📜  K’th臂数

📅  最后修改于: 2021-04-29 15:58:43             🧑  作者: Mango

动臂号是仅由数字2和3组成的数字。给定整数k(0 例子:

Input : k = 2
Output: 3

Input : k = 3
Output: 22

Input : k = 100
Output: 322323

Input: k = 1000000
Output: 3332322223223222223

参考: http //www.spoj.com/problems/TSHOW1/
这个想法很简单,例如生成二进制数。这里我们也使用相同的方法,
我们使用队列数据结构来解决这个问题。首先将“ 2”排队,然后将“ 3”排队,这两个分别是第一臂杆编号和第二臂杆编号。现在将count = 2设置为每次pop()在队列前面,并在弹出的数字后加上“ 2”,并增加计数++++(如果(count == k),然后打印当前的动臂编号,否则在弹出的数字后加上“ 3”,并增加计数++++,如果(count == k),然后打印当前的动臂编号。重复该过程,直到达到K’th Boom号
这种方法可以看作是根为空字符串的树的BFS。每个节点的左子节点附加2个,右子节点附加3个。

以下是此想法的实现。

C++
// C++ program to find K'th Boom number
#include
using namespace std;
typedef long long int ll;
 
// This function uses queue data structure to K'th
// Boom number
void  boomNumber(ll k)
{
    // Create an empty queue of strings
    queue q;
 
    // Enqueue an empty string
    q.push("");
 
    // counter for K'th element
    ll count = 0;
 
    // This loop checks the value of count to
    // become equal to K when value of count
    // will be equals to k we will print the
    // Boom number
    while (count <= k)
    {
        // current Boom number
        string s1 = q.front();
 
        // pop front
        q.pop();
 
        // Store current Boom number before changing it
        string s2 = s1;
 
        // Append "2" to string s1 and enqueue it
        q.push(s1.append("2"));
        count++;
 
        // check if count==k
        if (count==k)
        {
            cout << s1 << endl; // K'th Boom number
            break;
        }
 
        // Append "3" to string s2 and enqueue it.
        // Note that s2 contains the previous front
        q.push(s2.append("3"));
        count++;
 
        // check if count==k
        if (count==k)
        {
            cout << s2 << endl; // K'th Boom number
            break;
        }
    }
    return ;
}
 
// Driver program to test above function
int main()
{
    ll k = 1000000;
    boomNumber(k);
    return 0;
}


C#
// C# program to find K'th Boom number
using System;
using System.Collections;
 
class GFG{
 
// This function uses queue data structure
// to K'th Boom number
static void boomNumber(long k)
{
     
    // Create an empty queue of strings
    Queue q = new Queue();
  
    // Enqueue an empty string
    q.Enqueue("");
  
    // counter for K'th element
    long count = 0;
  
    // This loop checks the value of count to
    // become equal to K when value of count
    // will be equals to k we will print the
    // Boom number
    while (count <= k)
    {
         
        // current Boom number
        string s1 = (string)q.Dequeue();
  
        // Store current Boom number
        // before changing it
        string s2 = s1;
  
        // Append "2" to string s1 and
        // enqueue it
        s1 += "2";
        q.Enqueue(s1);
        count++;
  
        // Check if count==k
        if (count == k)
        {
             
            // K'th Boom number
            Console.Write(s1);
            break;
        }
  
        // Append "3" to string s2 and enqueue it.
        // Note that s2 contains the previous front
        s2 += "3";
        q.Enqueue(s2);
        count++;
  
        // Check if count==k
        if (count == k)
        {
             
            // K'th Boom number
            Console.Write(s2);
            break;
        }
    }
    return;
}   
 
// Driver code   
public static void Main(string []arg)
{
    long k = 1000000;
     
    boomNumber(k);
}
}
 
// This code is contributed by rutvik_56


输出:

3332322223223222223