How to identify a specific number in any numerical value?

Asked

Viewed 2,741 times

5

For example, my variable valor (whole type) has the value 1354, note that the number 13 appears at this numeric value. How can I identify a specific number at any other numeric value? I’d like an example in C.

Follows the statement of the problem:

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

I’ve identified a way to check if the last digit ends with 4, follow 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 doubt is how to identify 13 and 4 in any number.

  • Do you want to know if there is a sequence of digits within the digits of a number? Have you done anything? Are you having any specific questions? Any problem with what you’re doing? Show what you tried.

  • The problem here is to identify if the last digit ends with 4, this I solved, already the one I mentioned is to identify the number 13 in a certain value, for example: 413 has the number 13 already 414 has not, there must be some mathematical function to solve this.

  • Just need to see if you are at the end? Or you can be in any position?

  • In any position, for example: 42134 has the 13 in this value. It’s kind of a search within a value whole.

  • 1

    I wouldn’t do it mathematically, I’d convert to string and would search it. So I showed how the other one did, how you’re trying to do this.

  • Was to edit the question and put the enunciated.

Show 1 more comment

2 answers

7


Convert to string and search 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: http://ideone.com/K1WyIc

  • I’ll test it here.

  • 1

    Thank you for the reply.

  • It is interesting to clarify why the conversion to string is necessary. " 1354" for the computer is not the number, but the representation of a number (internalized as binary) on base 10. Saying that "1354" contains "13" makes sense for the number representation, but not for the value itself.

6

The solution to the above problem is to use the @Alexandre Borela function 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;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.