Encuentra la posición del número dado entre los números formados por 4 y 7

Considere una serie de números compuesta únicamente por los dígitos 4 y 7. Los primeros números de la serie son 4, 7, 44, 47, 74, 77, 444, .. etc. Dado un número formado por 4, 7 dígitos únicamente, necesitamos encontrar la posición de este número en esta serie.

Ejemplos: 

C++

// C++ program to find position of a number
// in a series of numbers with 4 and 7 as the
// only digits.
#include <iostream>
#include <algorithm>
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<j):
         
        # check all digit position
        # if number is left then
        # pos*2+1
        if(n[i] == '4'):
            pos = pos * 2 + 1
             
        # if number is right then
        # pos*2+2
        if(n[i] == '7'):
            pos = pos * 2 + 2
         
        i= i+1
     
    return pos
 
 
# Driver code
# given a number which is constructed
# by 4 and 7 digit only
n = "774"
print(findpos(n))
 
# This code is contributed by Sam007

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

<?php
// PHP program to find position of a number
// in a series of numbers with 4 and 7 as the
// only digits.
 
function findpos($n)
{
    $i = 0;
    $pos = 0;
    while($i < strlen($n)) {
 
        // 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
    // given a number which
    // is constructed by 4
    // and 7 digit only
    $n = "774";
    echo findpos($n);
     
// This code is contributed by Sam007
?>

Javascript

<script>
    // Javascript program to find position of a number
// in a series of numbers with 4 and 7 as the
// only digits.
   
function findpos(n)
{
    let i = 0;
    let pos = 0;
    while(i < 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++;
    }
    return pos;
}
   
    // Driver code
    // given a number which
    // is constructed by 4
    // and 7 digit only
    let n = "774";
    document.write(findpos(n));
       
// This code is contributed by _saurabh_jaiswal
</script>

Publicación traducida automáticamente

Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *