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

📅  最后修改于: 2021-05-31 20:56:48             🧑  作者: Mango

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

给定数字N。任务是找到给定序列中的第n个数字。
例子

Input : N = 2
Output : 5

Input : N = 5
Output : 53

该想法基于以下事实:序列中最后一位的值交替出现。例如,如果第i数字的最后一位为3,则第(i-1)(i + 1)数字的最后一位必须为5。
创建一个大小为(n + 1)的数组,然后将其推入3和5(这两个始终是序列的前两个元素)。有关更多元素的检查,

1) If i is odd,
      arr[i] = arr[i/2]*10 + 3;
2) If it is even,
      arr[i] = arr[(i/2)-1]*10 + 5;
At last return arr[n].

下面是上述想法的实现:

C++
// C++ program to find n-th number in a series
// made of digits 3 and 5
 
#include 
using namespace std;
 
// Function to find n-th number in series
// made of 3 and 5
int printNthElement(int n)
{
    // create an array of size (n+1)
    int arr[n + 1];
    arr[1] = 3;
    arr[2] = 5;
 
    for (int i = 3; i <= n; i++) {
        // If i is odd
        if (i % 2 != 0)
            arr[i] = arr[i / 2] * 10 + 3;
        else
            arr[i] = arr[(i / 2) - 1] * 10 + 5;
    }
    return arr[n];
}
 
// Driver code
int main()
{
    int n = 6;
 
    cout << printNthElement(n);
 
    return 0;
}


Java
// Java program to find n-th number in a series
// made of digits 3 and 5
 
class FindNth {
    // Function to find n-th number in series
    // made of 3 and 5
    static int printNthElement(int n)
    {
        // create an array of size (n+1)
        int arr[] = new int[n + 1];
        arr[1] = 3;
        arr[2] = 5;
 
        for (int i = 3; i <= n; i++) {
            // If i is odd
            if (i % 2 != 0)
                arr[i] = arr[i / 2] * 10 + 3;
            else
                arr[i] = arr[(i / 2) - 1] * 10 + 5;
        }
        return arr[n];
    }
 
    // main function
    public static void main(String[] args)
    {
        int n = 6;
 
        System.out.println(printNthElement(n));
    }
}


Python3
# Python3 program to find n-th number 
# in a series made of digits 3 and 5
   
# Return n-th number in series made 
# of 3 and 5
def printNthElement(n) :
       
    # create an array of size (n + 1)
    arr =[0] * (n + 1);
    arr[1] = 3
    arr[2] = 5
   
    for i in range(3, n + 1) :
        # If i is odd
        if (i % 2 != 0) :
            arr[i] = arr[i // 2] * 10 + 3
        else :
            arr[i] = arr[(i // 2) - 1] * 10 + 5
       
    return arr[n]
       
# Driver code
n = 6
print(printNthElement(n))


C#
// C# program to find n-th number
// in a series made of digits 3 and 5
using System;
 
class GFG
{
// Function to find n-th number
// in series made of 3 and 5
static int printNthElement(int n)
{
    // create an array of size (n+1)
    int [] arr = new int[n + 1];
    arr[1] = 3;
    arr[2] = 5;
 
    for (int i = 3; i <= n; i++)
    {
        // If i is odd
        if (i % 2 != 0)
            arr[i] = arr[i / 2] * 10 + 3;
        else
            arr[i] = arr[(i / 2) - 1] * 10 + 5;
    }
    return arr[n];
}
 
// Driver Code
static void Main()
{
    int n = 6;
 
    Console.WriteLine(printNthElement(n));
}
}
 
// This code is contributed by ANKITRAI1


PHP


Javascript


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