📜  在由4和7构成的数字中找到给定数字的位置

📅  最后修改于: 2021-04-23 07:25:27             🧑  作者: Mango

考虑一个仅由数字4和7组成的数字系列。该系列中的前几个数字是4、7、44、47、74、77、444等。等等。给定一个仅由4、7个数字构成的数字,我们需要找到该数字在本系列中的位置。

例子:

Input : 7
Output : pos = 2 

Input : 444
Output : pos = 7

它与以下文章相反:
在只允许使用2位数字(4和7)的序列中查找第n个元素|集合2(log(n)方法)

""
               /              \
             1(4)            2(7)
          /        \       /      \ 
        3(44)    4(47)   5(74)    6(77)
       / \       / \      / \      / \

该想法基于这样一个事实,即所有偶数定位数字的最后一位数字均为7,所有奇数定位数字的最后一位数字均为4。

如果数字是4,则它是树的左节点,则它对应于(pos * 2)+1。其他右子节点(7)对应于(pos * 2)+2。

C++
// C++ program to find position of a number
// in a series of numbers with 4 and 7 as the
// only digits.
#include 
#include 
using namespace std;
  
int findpos(string n)
{
    int i = 0, pos = 0;
    while (n[i] != '\0') {
  
        // check all digit position
        switch (n[i]) 
        {
  
        // if number is left then pos*2+1
        case '4':
            pos = pos * 2 + 1;
            break;
  
        // if number is right then pos*2+2
        case '7':
            pos = pos * 2 + 2; 
            break;
        }
        i++;
    }
    return pos;
}
  
// Driver code
int main()
{ 
    // given a number which is constructed 
    // by 4 and 7 digit only 
    string n = "774"; 
    cout << findpos(n);
    return 0;
}


Java
// java program to find position of a number
// in a series of numbers with 4 and 7 as the
// only digits.
import java.util.*;
  
class GFG {
      
    static int findpos(String n)
    {
          
        int k = 0, pos = 0, i = 0;
        while (k != n.length()) {
  
            // check all digit position
            switch (n.charAt(i)) {
  
            // if number is left then pos*2+1
            case '4':
                pos = pos * 2 + 1;
                break;
  
            // if number is right then pos*2+2
            case '7':
                pos = pos * 2 + 2;
                break;
            }
              
            i++;
            k++;
        }
          
        return pos;
    }
  
    // Driver code
    public static void main(String[] args)
    {
          
        // given a number which is constructed
        // by 4 and 7 digit only
        String n = "774";
          
        System.out.println(findpos(n));
    }
}
  
// This code is contributed by Sam007.


Python3
# python program to find position 
# of a number in a series of 
# numbers with 4 and 7 as the
# only digits.
def findpos(n):
    i = 0
    j = len(n)
    pos = 0
    while (i


C#
// C# program to find position of 
// a number in a series of numbers
// with 4 and 7 as the only digits.
using System;
  
class GFG
{
    static int findpos(String n)
    {
          
        int k = 0, pos = 0, i = 0;
        while (k != n.Length) {
  
            // check all digit position
            switch (n[i]) {
  
            // if number is left then pos*2+1
            case '4':
                pos = pos * 2 + 1;
                break;
  
            // if number is right then pos*2+2
            case '7':
                pos = pos * 2 + 2;
                break;
            }
              
            i++;
            k++;
        }
          
        return pos;
    }
  
    // Driver code
    static void Main()
    {
          
        // given a number which is constructed
        // by 4 and 7 digit only
        String n = "774";
          
        Console.Write(findpos(n));
    }
      
}
  
// This code is contributed by Sam007


PHP


输出:

13