📌  相关文章
📜  在只允许数字4和7形成的序列中查找给定术语的位置

📅  最后修改于: 2021-06-01 01:08:59             🧑  作者: Mango

有一系列数字只有数字4和7,并且数字以升序排列。系列的前几个数字是4、7、44、47、74、77、444等。给定数字N ,任务是找到该数字在给定系列中的位置。
例子:

方法:可以使用以下观察结果解决此问题:

  • 以下是给定系列中观察到的模式。这些数字可以看成是:
""
              /      \
            4         7
          /   \     /   \ 
        44    47   74    77
       / \   / \   / \  / \
  • 如我们所见,模式的2的幂增加。因此,想法是从最低有效数字开始迭代数字的数字,并将数字的位置更新为:
    1. 如果当前数字= 7,则添加2^{height + 1}   到该位置。
    2. 如果当前数字= 4,则添加2^{height}   到该位置。
  • 完成上述操作后,打印最终位置。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the position
// of the number N
void findPosition(int n)
{
    int i = 0;
 
    // To store the position of N
    int pos = 0;
 
    // Iterate through all digit of N
    while (n > 0) {
 
        // If current digit is 7
        if (n % 10 == 7) {
            pos = pos + pow(2, i + 1);
        }
 
        // If current digit is 4
        else {
            pos = pos + pow(2, i);
        }
 
        i++;
        n = n / 10;
    }
 
    // Print the final position
    cout << pos;
}
 
// Driver Code
int main()
{
    // Given number of the series
    int N = 777;
 
    // Function Call
    findPosition(N);
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
class GFG{
 
// Function to find the position
// of the number N
static void findPosition(int n)
{
    int i = 0;
 
    // To store the position of N
    int pos = 0;
 
    // Iterate through all digit of N
    while (n > 0)
    {
 
        // If current digit is 7
        if (n % 10 == 7)
        {
            pos = pos + (int)Math.pow(2, i + 1);
        }
 
        // If current digit is 4
        else
        {
            pos = pos + (int)Math.pow(2, i);
        }
 
        i++;
        n = n / 10;
    }
 
    // Print the final position
    System.out.print(pos);
}
 
// Driver Code
public static void main(String[] args)
{
    // Given number of the series
    int N = 777;
 
    // Function Call
    findPosition(N);
}
}
 
// This code is contributed by shivanisinghss2110


Python3
# Python3 program for the above approach
 
# Function to find the position
# of the number N
def findPosition(n):
     
    i = 0
 
    # To store the position of N
    pos = 0
 
    # Iterate through all digit of N
    while (n > 0):
 
        # If current digit is 7
        if (n % 10 == 7):
            pos = pos + pow(2, i + 1)
 
        # If current digit is 4
        else:
            pos = pos + pow(2, i)
             
        i += 1
        n = n // 10
 
    # Print the final position
    print(pos)
 
# Driver Code
if __name__ == '__main__':
     
    # Given number of the series
    N = 777
 
    # Function Call
    findPosition(N)
 
# This code is contributed by mohit kumar 29


C#
// C# program for the above approach
using System;
class GFG{
 
// Function to find the position
// of the number N
static void findPosition(int n)
{
    int i = 0;
 
    // To store the position of N
    int pos = 0;
 
    // Iterate through all digit of N
    while (n > 0)
    {
 
        // If current digit is 7
        if (n % 10 == 7)
        {
            pos = pos + (int)Math.Pow(2, i + 1);
        }
 
        // If current digit is 4
        else
        {
            pos = pos + (int)Math.Pow(2, i);
        }
 
        i++;
        n = n / 10;
    }
 
    // Print the final position
    Console.Write(pos);
}
 
// Driver Code
public static void Main()
{
    // Given number of the series
    int N = 777;
 
    // Function Call
    findPosition(N);
}
}
 
// This code is contributed by Code_Mech


Javascript


输出:
14

时间复杂度: O(log 10 N)