número fascinante

Dado un número N, la tarea es comprobar si es fascinante o no. 
Número fascinante : cuando un número (3 dígitos o más) se multiplica por 2 y 3, y cuando ambos productos se concatenan con el número original, todos los dígitos del 1 al 9 se presentan exactamente una vez. Puede haber cualquier número de ceros y se ignoran. 
Ejemplos: 
 

Entrada: 192 
Salida: Sí 
Después de la multiplicación por 2 y 3, y concatenación con el número original, el número resultante es 192384576 que contiene todos los dígitos del 1 al 9.
Entrada: 853 
Salida: No 
Después de la multiplicación por 2 y 3, y concatenación con el número original , el número resultante es 85317062559. En esto, falta el número 4 y el número 5 ha aparecido varias veces. 
 

Enfoque
 

  1. Compruebe si el número dado tiene tres dígitos o más. Si no, imprima No.
  2. Si no, multiplica el número dado por 2 y 3.
  3. Concatene estos productos con el número dado para formar una string.
  4. Atraviese esta string, mantenga el conteo de frecuencia de los dígitos.
  5. Imprima No si falta algún dígito o ha aparecido varias veces.
  6. De lo contrario, imprima Sí.

A continuación se muestra la implementación del enfoque anterior: 
 

C++14

// C++ program to implement
// fascinating number
#include <bits/stdc++.h>
using namespace std;
 
// function to check if number
// is fascinating or not
bool isFascinating(int num)
{
    // frequency count array
    // using 1 indexing
    int freq[10] = {0};
 
    // obtaining the resultant number
    // using string concatenation
    string val = "" + to_string(num) +
                      to_string(num * 2) +
                      to_string(num * 3);
 
    // Traversing the string
    // character by character
    for (int i = 0; i < val.length(); i++)
    {
 
        // gives integer value of
        // a character digit
        int digit = val[i] - '0';
 
        // To check if any digit has
        // appeared multiple times
        if (freq[digit] and digit != 0 > 0)
            return false;
        else
            freq[digit]++;
    }
 
    // Traversing through freq array to
    // check if any digit was missing
    for (int i = 1; i < 10; i++)
    {
        if (freq[i] == 0)
            return false;
    }
    return true;
}
 
// Driver code
int main()
{
    // Input number
    int num = 192;
 
    // Not a valid number
    if (num < 100)
        cout << "No" << endl;
 
    else
    {
        // Calling the function to
        // check if input number
        // is fascinating or not
        bool ans = isFascinating(num);
        if (ans)
            cout << "Yes";
        else
            cout << "No";
    }
}
 
// This code is contributed
// by Subhadeep

Java

// Java program to implement
// fascinating number
import java.io.*;
import java.util.*;
 
public class GFG {
 
    // function to check if number
    // is fascinating or not
    public static boolean isFascinating(
                                int num)
    {
         
        // frequency count array
        //using 1 indexing
        int[] freq = new int[10];
 
        // obtaining the resultant number
        // using string concatenation
        String val = "" + num + num * 2 +
                                num * 3;
 
        // Traversing the string character //by character
        for (int i = 0; i < val.length(); i++)
        {
 
            // gives integer value of //a character digit
            int digit = val.charAt(i) - '0';
 
            // To check if any digit has
            // appeared multiple times
            if (freq[digit]>0 && digit != 0)
                return false;
            else
                freq[digit]++;
        }
 
        // Traversing through freq array to
        // check if any digit was missing
        for (int i = 1; i < freq.length; i++)
        {
            if (freq[i] == 0)
                return false;
        }
        return true;
    }
 
    // Driver code
    public static void main(String args[])
    {
         
        // Input number
        int num = 192;
 
        // Not a valid number
        if (num < 100)
            System.out.println("No");
 
        else
        {
             
            // Calling the function to check
            // if input number is fascinating or not
            boolean ans = isFascinating(num);
            if (ans)
                System.out.println("Yes");
            else
                System.out.println("No");
        }
    }
}

Python 3

# Python 3 program to implement
# fascinating number
 
# function to check if number
# is fascinating or not
def isFascinating(num) :
 
    # frequency count array
    # using 1 indexing
    freq = [0] * 10
 
    # obtaining the resultant number
    # using string concatenation
    val = (str(num) + str(num * 2) +
                      str(num * 3))
 
    # Traversing the string
    # character by character
    for i in range(len(val)) :
 
        # gives integer value of
        # a character digit
        digit = int(val[i])
 
        # To check if any digit has
        # appeared multiple times
        if freq[digit] and digit != 0 > 0 :
            return False
        else :
            freq[digit] += 1
 
    # Traversing through freq array to
    # check if any digit was missing
    for i in range(1, 10) :
 
        if freq[i] == 0 :
            return False
 
    return True
 
# Driver Code
if __name__ == "__main__" :
 
    # Input number
    num = 192
 
    # Not a valid number
    if num < 100 :
        print("No")
 
    else :
         
        # Calling the function to
        # check if input number
        # is fascinating or not
        ans = isFascinating(num)
         
        if ans :
            print("Yes")
        else :
            print("No")
 
# This code is contributed by ANKITRAI1

C#

// C# program to implement
// fascinating number
using System;
 
class GFG
{
 
// function to check if number
// is fascinating or not
public static bool isFascinating(int num)
{
    // frequency count array
    // using 1 indexing
    int[] freq = new int[10];
 
    // obtaining the resultant number
    // using string concatenation
    String val = "" + num.ToString() +
                     (num * 2).ToString() +
                     (num * 3).ToString();
 
    // Traversing the string
    // character by character
    for (int i = 0; i < val.Length; i++)
    {
 
        // gives integer value of
        // a character digit
        int digit = val[i] - '0';
 
        // To check if any digit has
        // appeared multiple times
        if (freq[digit] && digit != 0 > 0 )
            return false;
        else
            freq[digit]++;
    }
 
    // Traversing through freq array to
    // check if any digit was missing
    for (int i = 1; i < freq.Length; i++)
    {
        if (freq[i] == 0)
            return false;
    }
    return true;
}
 
// Driver code
static void Main()
{
    // Input number
    int num = 192;
 
    // Not a valid number
    if (num < 100)
        Console.WriteLine("No");
 
    else
    {
        // Calling the function to check
        // if input number is fascinating or not
        bool ans = isFascinating(num);
        if (ans)
            Console.WriteLine("Yes");
        else
            Console.WriteLine("No");
    }
}
}
 
// This code is contributed by mits

PHP

<?php
// PHP program to implement
// fascinating number
 
// function to check if number
// is fascinating or not
function isFascinating($num)
{
    // frequency count array
    // using 1 indexing
    $freq = array_fill(0, 10, NULL);
 
    // obtaining the resultant number
    // using string concatenation
    $val = "" . $num . ($num * 2).
                       ($num * 3);
 
    // Traversing the string
    // character by character
    for ($i = 0; $i < strlen($val); $i++)
    {
 
        // gives integer value of
        // a character digit
        $digit = $val[$i] - '0';
 
        // To check if any digit has
        // appeared multiple times
        if ($freq[$digit] > 0 && $digit != 0)
            return false;
        else
            $freq[$digit]++;
    }
 
    // Traversing through freq array to
    // check if any digit was missing
    for ($i = 1; $i < 10; $i++)
    {
        if ($freq[$i] == 0)
            return false;
    }
    return true;
}
 
// Driver code
 
// Input number
$num = 192;
 
// Not a valid number
if ($num < 100)
    echo "No" ;
 
else
{
    // Calling the function to
    // check if input number
    // is fascinating or not
    $ans = isFascinating($num);
    if ($ans)
        echo "Yes";
    else
        echo "No";
}
 
// This code is contributed
// by ChitraNayal
?>

Javascript

<script>
 
// Javascript program to implement
// fascinating number
     
    // function to check if number
    // is fascinating or not
    function isFascinating(num)
    {
        // frequency count array
        //using 1 indexing
        let freq = new Array(10);
        for(let i=0;i<freq.length;i++)
        {
            freq[i]=0;
        }
  
        // obtaining the resultant number
        // using string concatenation
        let val = "" + num + num * 2 +
                                num * 3;
  
        // Traversing the string character //by character
        for (let i = 0; i < val.length; i++)
        {
  
            // gives integer value of //a character digit
            let digit = val[i].charCodeAt(0) -
            '0'.charCodeAt(0);
  
            // To check if any digit has
            // appeared multiple times
            if (freq[digit]>0 && digit != 0)
                return false;
            else
                freq[digit]++;
        }
  
        // Traversing through freq array to
        // check if any digit was missing
        for (let i = 1; i < freq.length; i++)
        {
            if (freq[i] == 0)
                return false;
        }
        return true;
    }
     
    // Driver code
     
     
    // Input number
    let num = 192;
    // Not a valid number
    if (num < 100)
        document.write("No");
  
    else
    {
              
        // Calling the function to check
        // if input number is fascinating or not
        let ans = isFascinating(num);
        if (ans)
            document.write("Yes");
        else
            document.write("No");
    }
 
// This code is contributed by rag2127
 
</script>
Producción: 

Yes

 

Publicación traducida automáticamente

Artículo escrito por rachana soma 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 *