📌  相关文章
📜  程序找到系列3、5、33、35、53…的第N个项。 |套装2

📅  最后修改于: 2021-05-25 09:46:07             🧑  作者: Mango

给定一系列仅由数字3和5组成的数字。该系列中的前几个数字是:

例子:

Input: N = 2
Output: 5

Input: N = 5
Output: 53

对于O(n)解,请参考程序以找到第3、5、33、35、53系列的第N个项。在此帖子中,将讨论O(log n)解,它基于以下数字模式。可以看到数字。

""
              /      \
            3         5
          /   \     /   \ 
        33    35   53    55
       / \   / \   / \  / \

这个想法是从头开始填写所需的数字。我们知道可以观察到,如果n为奇数,则最后一位为3,如果n为偶数,则最后一位为5。填完最后一位数字后,我们将移动到树中的父节点。如果n为奇数,则父节点对应于(n-1)/ 2。其他父节点对应于(n-2)/ 2。

C++
// C++ program to find n-th number containing
// only 3 and 5.
#include 
using namespace std;
 
string findNthNo(int n)
{
    string res = "";
    while (n >= 1) {
        // If n is odd, append 3 and
        // move to parent
        if (n & 1) {
            res = res + "3";
            n = (n - 1) / 2;
        }
 
        // If n is even, append 5 and
        // move to parent
        else {
            res = res + "5";
            n = (n - 2) / 2;
        }
    }
 
    // Reverse res and return.
    reverse(res.begin(), res.end());
    return res;
}
 
// Driver code
int main()
{
    int n = 5;
    cout << findNthNo(n);
    return 0;
}


Java
// java program to find n-th number
// containing only 3 and 5.
public class GFG {
 
    static String findNthNo(int n)
    {
        String res = "";
        while (n >= 1) {
 
            // If n is odd, append
            // 3 and move to parent
            if ((n & 1) == 1) {
                res = res + "3";
                n = (n - 1) / 2;
            }
 
            // If n is even, append
            // 5 and move to parent
            else {
                res = res + "5";
                n = (n - 2) / 2;
            }
        }
 
        // Reverse res and return.
        StringBuilder sb = new StringBuilder(res);
        sb.reverse();
        return new String(sb);
    }
 
    // Driver code
    public static void main(String args[])
    {
        int n = 5;
 
        System.out.print(findNthNo(n));
    }
}


Python3
# Python3 program to find
# n-th number containing
# only 3 and 5.
def reverse(s):
    if len(s) == 0:
        return s
    else:
        return reverse(s[1:]) + s[0]
           
def findNthNo(n):
    res = "";
    while (n >= 1):
           
        # If n is odd, append
        # 3 and move to parent
        if (n & 1):
            res = res + "3";
            n = (int)((n - 1) / 2);
               
            # If n is even, append 5
            # and move to parent
        else:
            res = res + "5";
            n = (int)((n - 2) / 2);
               
    # Reverse res
    # and return.
    return reverse(res);
   
# Driver code
n = 5;
print(findNthNo(n));


C#
// C# program to find n-th number
// containing only 3 and 5.
using System;
 
class GFG
{
// function to reverse a string
public static string Reverse(string s)
{
    char[] charArray = s.ToCharArray();
    Array.Reverse(charArray);
    return new string(charArray);
}
 
// function to find nth number
static string findNthNo(int n)
{
    string res = "";
    while (n >= 1)
    {
 
        // If n is odd, append
        // 3 and move to parent
        if ((n & 1) == 1)
        {
            res = res + "3";
            n = (n - 1) / 2;
        }
 
        // If n is even, append
        // 5 and move to parent
        else
        {
            res = res + "5";
            n = (n - 2) / 2;
        }
    }
 
    string sb = Reverse(res) ;
    return sb ;
}
 
// Driver code
static void Main()
{
    int n = 5;
 
    Console.WriteLine(findNthNo(n));
}
}
 
// This code is contributed by ANKITRAI1


PHP
= 1)
    {
        // If n is odd, append 3
        // and move to parent
        if ($n & 1)
        {
            $res = $res + "3";
            $n = ($n - 1) / 2;
        }
 
        // If n is even, append 5
        // and move to parent
        else
        {
            $res = $res . "5";
            $n = ($n - 2) / 2;
        }
    }
 
    // Reverse res and return.
    $res = strrev($res);
    return $res;
}
 
// Driver code
$n = 5;
echo findNthNo($n);
 
// This code is contributed
// by ChitraNayal
?>


Javascript


输出:
53