# 在只允许使用2位数字(4和7)的序列中查找第n个元素|S2 (log(n)方法)

2021年4月8日17:35:09 发表评论 612 次浏览

## 本文概述

``````Input : n = 2
Output : 7

Input : n = 3
Output : 44

Input  : n = 5
Output : 74

Input  : n = 6
Output : 77``````

``````""
/     \
4         7
/  \     /  \
44    47   74    77
/\   /\   /\  /\``````

## C ++

``````//C++ program to find n-th number containing
//only 4 and 7.
#include<bits/stdc++.h>
using namespace std;

string findNthNo( int n)
{
string res = "" ;
while (n>= 1)
{
//If n is odd, append 4 and
//move to parent
if (n & 1)
{
res = res + "4" ;
n = (n-1)/2;
}

//If n is even, append 7 and
//move to parent
else
{
res = res + "7" ;
n = (n-2)/2;
}
}

//Reverse res and return.
reverse(res.begin(), res.end());
return res;
}

//Driver code
int main()
{
int n = 13;
cout <<findNthNo(n);
return 0;
}``````

## Java

``````//java program to find n-th number
//containing only 4 and 7.
public class GFG {

static String findNthNo( int n)
{
String res = "" ;
while (n>= 1 )
{

//If n is odd, append
//4 and move to parent
if ((n & 1 ) == 1 )
{
res = res + "4" ;
n = (n - 1 ) /2 ;
}

//If n is even, append
//7 and move to parent
else
{
res = res + "7" ;
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 = 13 ;

System.out.print( findNthNo(n) );
}
}

//This code is contributed by Sam007``````

## Python3

``````# Python3 program to find
# n-th number containing
# only 4 and 7.
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
# 4 and move to parent
if (n & 1 ):
res = res + "4" ;
n = ( int )((n - 1 ) /2 );

# If n is even, append7
# and move to parent
else :
res = res + "7" ;
n = ( int )((n - 2 ) /2 );

# Reverse res
# and return.
return reverse(res);

# Driver code
n = 13 ;
print (findNthNo(n));

# This code is contributed
# by mits``````

## C#

``````//C# program to find n-th number
//containing only 4 and 7.
using System;
class GFG {

static string findNthNo( int n)
{
string res = "" ;
while (n>= 1)
{

//If n is odd, append 4 and
//move to parent
if ((n & 1) == 1)
{
res = res + "4" ;
n = (n - 1) /2;
}

//If n is even, append 7 and
//move to parent
else
{
res = res + "7" ;
n = (n - 2) /2;
}
}

//Reverse res and return.
char [] arr = res.ToCharArray();
Array.Reverse(arr);
return new string (arr);

}

//Driver Code
public static void Main()
{
int n = 13;
Console.Write( findNthNo(n) );
}
}

//This code is contributed by Sam007``````

## 的PHP

``````<?php
//PHP program to find
//n-th number containing
//only 4 and 7.

function findNthNo( \$n )
{
\$res = "" ;
while ( \$n>= 1)
{
//If n is odd, append
//4 and move to parent
if ( \$n & 1)
{
\$res = \$res . "4" ;
\$n = (int)(( \$n - 1) /2);
}

//If n is even, append
//7 and move to parent
else
{
\$res = \$res . "7" ;
\$n = (int)(( \$n - 2) /2);
}
}

//Reverse res
//and return.
return strrev ( \$res );
}

//Driver code
\$n = 13;
echo findNthNo( \$n );

//This code is contributed
//by mits
?>``````

``774``