How to identify a specific number in any numeric value?

5

For example, my variable valor has the value 1354 , note that 13 appears in this numeric value . How can I identify a specific number in any other numeric value? I would like an example in C.

The following is the problem statement:

China is building a space elevator, which will allow the launch of probes and satellites to a much lower cost, enabling not only scientific research projects such as space tourism. However, the Chinese are very superstitious, so they are very lift floors: they do not use any number that contains the digit "4" or sequence of digits "13". Thus, they do not use floor 4, floor 13, floor 134, nor floor floor 113, but use floor 103. Thus, the first floors are numbered 1, 2, 3, 5, 6, 7, 8, 9, 10, 11, 12, 15, 16. . . As the space elevator has many floors, and they need to number all floors of the elevator, the Chinese asked you to write a program that, given the floor, indicates the number you should be assigned to it

I have identified a way to check if the last digit ends with 4, follows the example:

int andar(int numero)
{
    int sobra = 0;
    int resultado;

    sobra = numero % 10;

    if (sobra == 4)
    {
        resultado = numero + 1;
    }
    else
    {
        resultado = numero;
    }

    return resultado;
}

My question is how to identify 13 and 4 in any number.

    
asked by anonymous 30.08.2015 / 01:33

2 answers

7

Converts to string and searches for substring.

#include <stdio.h>
#include <string.h>

int ProcuraNumero (int NumeroProcurado, int NumeroAlvo) {
    char NumeroProcuradoString[16];
    char NumeroAlvoString[16];

    sprintf (NumeroProcuradoString, "%d", NumeroProcurado);
    sprintf (NumeroAlvoString, "%d", NumeroAlvo);

    return strstr (NumeroAlvoString, NumeroProcuradoString) != NULL ? 1 : 0;
}

int main(void) {
    printf("%d:", ProcuraNumero (13, 1354)); // 1 = Encontrou.
    printf("%d:", ProcuraNumero (13, 1054)); // 0 = Não encontrou.
    return 0;
}

Example running here: link

    
30.08.2015 / 01:58
6

The solution to the above problem is to use the @Alexandre function Borela ProcuraNumero(int NumeroProcurado, int NumeroAlvo) in a loop for .

Solution:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int ProcuraNumero(int, int);

int main(void)
{
    int n_andar , i, treze, quatro;

    printf("\nNumero do andar: ");
    scanf("%i", &n_andar);

    for (i = 0; i < n_andar; i++)
    {
        treze = ProcuraNumero(13, n_andar);

        quatro = ProcuraNumero(4, n_andar);

        if ((quatro == 1) || (treze == 1))
        {
            n_andar += 1;
        }
    }

    printf("\nNovo numero do andar: %i\n\n", n_andar);
}

int ProcuraNumero(int NumeroProcurado, int NumeroAlvo)
{
    int resultado;

    char NumeroProcuradoString[16];
    char NumeroAlvoString[16];

    sprintf(NumeroProcuradoString, "%d", NumeroProcurado);
    sprintf(NumeroAlvoString, "%d", NumeroAlvo);

    resultado = strstr(NumeroAlvoString, NumeroProcuradoString) != NULL ? 1 : 0;

    return resultado;
}
    
30.08.2015 / 03:12