I cannot prepare the calculation to solve the problem proposed by the enunciation

Asked

Viewed 249 times

-8

I need to develop a program in C language, but I am beginner. :(

Follows the statement:

That is the context of the programme:

A smart cat walked into a messy room that needed cleaning. Instead of doing the job alone, he decides to transfer the work to his sidekick cats. The smart cat has a number of helper cats (smaller than him) inside his hat. Each helper cat also has its own helper cats (smaller than them) inside their hats. Thus, each helper cat transfers the work to be done to its own helpers. Eventually the helper cats reach a minimum size of 1. These cats of minimum size have no helpers in their hats: they are workers who must do the work themselves. Given the size of the first smart cat and the number of working cats (size 1), you should find the number of cats that do not perform any work (i.e., lazy cats larger than 1) and also determine the sum of the sizes of all cats.

Objective of the programme:

Make a program that reads from the keyboard two positive integers, T and Q. The first integer, T, indicates the size of the first smart cat and the second integer, Q, indicates the amount of working cats.

For each pair of numbers your program must produce the following output:

Quantidade de gatos que nao trabalham: 999
Tamanho total dos gatos: 999

Where 999 represents the values calculated by the program, according to the description of the problem, for the numbers read T and Q.

After printing the calculated values, your program must print a blank line and restart the procedure, ending only when 0 (zero) is entered for the T number. At the end of the processing your program should print the message "End of program".

Assumptions and restrictions

1) Before reading the T number, your program should print, on the same line, the message "Smart cat size: ", re-reading the T number (printing the orientation message again), if the entered value is negative.

2) Before reading the Q number, your program should print, on the same line, the message "Number of working cats: ", re-reading the Q number (printing the orientation message again), if the entered value is negative or zero.

3) For each pair of T and Q numbers the following rules apply: The number of cats inside each hat (of the cats that have helpers) is constant and equal to N. The size of cats inside a hat is 1/(N + 1) times the size of the cat that has the hat they are in. (For example, if N is equal to 3, the size of cats in a cat’s hat of size 16 is 4).

4) The numbers T and Q will always reflect a valid situation. That is, starting from a cat of size T, considering the cats in his hat and the cats in the hats of each helper, eventually one arrives at a quantity Q of working cats (size 1).

Example A)
The following output illustrates a valid execution of the program:

Tamanho do gato esperto: -32
Tamanho do gato esperto: -961
Tamanho do gato esperto: 216
Quantidade de gatos trabalhadores: 125
Quantidade de gatos que nao trabalham: 31
Tamanho total dos gatos: 671

Tamanho do gato esperto: 5764801
Quantidade de gatos trabalhadores: 0
Quantidade de gatos trabalhadores: -2148
Quantidade de gatos trabalhadores: 1679616
Quantidade de gatos que nao trabalham: 335923
Tamanho total dos gatos: 30275911

Tamanho do gato esperto: 0
Fim de programa

Here’s the code I already have:

#include<stdio.h>

int main (void){

    int T, Q;

    do{
        do{
            printf("Tamanho do gato esperto: ");
            scanf("%d", &T);
        } while(T < 0);

        if(T != 0){
            do{
                printf("Quantidade de gatos trabalhadores: ");
                scanf("%d", &Q);
            } while(Q <= 0);

            if((T > 0) && (Q > 0)){

                /* não consigo imaginar o calculo para descobrir os valores 
                de saída do programa */

                printf("Quantidade de gatos que nao trabalham: \n");
                printf("Tamanho total dos gatos: \n\n");

            }
        }
    } while(T != 0);

    if(T == 0){
        printf("Fim de programa\n");
    }

    return 0;
}

Doubt: I can’t imagine what would be the calculation to be performed to execute the output of the program example.

  • 6

    Danilo, I didn’t deny it, but let me explain, the goal here is not to do everything, in case your biggest problem is the initial calculation (at least that’s what I understood), if you edit the question and give a focus on the isolated problem the question could be saved, of course it’s just my opinion and I don’t know if the other members of the site will agree.

  • 2

    I agree as @Guilhermenascimento you can post the full code but the question must understand an isolated part of it, and when it was solved yet another question would be to open a new question.

  • 1

    This question seems valid to me. Could someone explain to me what is so wrong with it?

  • @Victorstafusa I did not find clear the question, and it is very difficult to isolate the problem of it and lack objectivity in relation to the calculation, at least for me this. So I voted as unclear, because I believe that there is no need to negative it, just a close and wait for the AP to make the question clearer.

  • @The cat problem is from the Q and the T reach the N and then the number of cats that do not work and the total size of cats. And turn that formula into an algorithm.

  • @Victorstafuses an issue by removing what is irrelevant in the question and addressing this formula and how to apply it in an algorithm would make it an optimal question.

  • 2

    @The problem is that this looks like college exercise, so anything that was removed could compromise the program’s understanding, and there’s a lot of teachers out there who go through very difficult exercises to decipher. In this case, I do not believe that it is the author’s fault.

  • @Victorstafusa think that giving a focus on the formulae on the part of the algorithm corresponding to it would help a lot, even keeping the text that this profuse just not to change the meaning of the question, at least separates the problem.

  • 1

    +1 for all irony related to the lazy cats in the hat.

  • @Lacobus is very "hate" against cats ;P

  • 2

    @Vitorstafusa you’re right, it’s college issue. I did not understand the reasoning of the teacher and I can not imagine a formula to arrive at the results of the example that has in the description of the problem ://

  • 3

    @Daniloantônio posted the answer. In fact, these formulas were something very difficult to get. Your teacher must be crazy.

  • 2

    @Daniloantônio @gato @Victorstafusa: This problem is called The Cat in the Hat and was originally proposed by a website called Uva Online Judge. I found in the github a code in C++ that maybe solve the problem.

Show 8 more comments

1 answer

7


First, define this function to simplify your code:

long long ler_numero(const char *mensagem) {
    long long n = -1;
    do {
        printf("%s: ", mensagem);
        scanf("%lld", &n);
    } while (n < 0);
    return n;
}

And then you can use it that way:

long long t = ler_numero("Tamanho do gato esperto");
long long q = ler_numero("Quantidade de gatos trabalhadores");

You may be wondering why long long. It turns out that long long is a 64-bit integer for storing very large numbers. And if you look down at the math, you’ll see that you’re dealing with really big numbers.

We know that the cats that work are size 1. Let’s try based on this to reach the size of other cats:

The size of cats inside a hat is 1/(N + 1) times the size of the cat that has the hat they are in.

That is, if Gk is the size of the cat inside the hat and Gk+1 is the size of the cat that has the hat, so:

Gnrec

Knowing that working cats are size 1, so:

G0=1

And therefore:

G1=xxx1
G1=xxx2
G1=xxx3

G2=xxx1
G2=xxx2
G2=xxx3

G3=xxx1
G3=xxx2
G3=xxx3

By induction we have to:

Gk=xxx

Your problem is finding the values of N and k, where k would be the smart cat level.

Let’s try the first case:

Gk=xxx

Replacing Gk for T:

Gk=216a

Fatorando the T:

Gk=216b
Gk=216c
Gk=216d
Gk=216e

Number of working cats: N^k

To calculate the number of cats that don’t work, if we have 125 working cats and N is 5, then those are in the hat of 25 cats that are in the hat of 5 cats that are in the hat of the smart cat. Soon:

25+5+1

Let’s try to generalize the formula:

N^2+N^1+N^0
z

Total size of cats:

total1

Generalizing:

totalx

Now let’s look at the second case:

Gk=xxx
Gk=5764801a
Gk=5764801b
Gk=5764801d
Gk=5764801e
Gk=5764801f
z2a
z2b
z2c
z2d
z2e
total2a
total2b
total2c
total2d
total2e
total2f

Well, now that we know the mathematical theory, how can it be implemented?

You will need a function to calculate power. For example:

long long potencia(long long base, long long expoente) {
    long long resposta = 1;
    // Use um for aqui para multiplicar base por base repetidas vezes.
    return resposta;
}

Calculating these summations can also be a little tricky, but you can do something like this:

long long gatos_que_nao_trabalham(long long k, long long i) {
    long long resposta = 0;
    // Use um for para fazer cada etapa do somatório. Veja a fórmula do z.
    return resposta;
}

long long tamanho_total_dos_gatos(long long k, long long i) {
    long long resposta = 0;
    // Use um for para fazer cada etapa do somatório. Veja a fórmula do f.
    // Lembre-se que em cada passo você tem que calcular tanto o N^(k-i) quanto o (N+1)^i.
    return resposta;
}

Having these functions, the problem is reduced to finding the k and the N. You know that T and Q are powers and the k is the exponent of the two, so you should look for the roots and finding, you have the N.

However, there may be several whole roots. To know which one you want, you use the one that works with both. Do something more or less like this:

long long raiz(long long a, long long b) {
    // Faça uma busca binária no intervalo de 2 até a
    // procurando por um número x tal que potencia(x, b) == a.
    // Se achar, retorne x.
    // Se não achar, retorne -1.
}

void acha_k_e_n(long long q, long long t, long long *k, long long *n) {
    for (*k = 0; *k < 64; *k++) {
        *n = raiz(t, *k) - 1;
        if (*n > 0 && potencia(*n, *k) == q) return;
    }
    *k = -1;
    *n = -1;
}

If that function gives k and n the values of -1, so there is no solution to the problem and you should abort (use a if for this). If not, proceed with the calculations of gatos_que_nao_trabalham and tamanho_total_dos_gatos.

  • 3

    +1 for the answer that saved the question :D

  • Pera, Doctor Seuss?

  • It should have a variable called sam_i_am, who should receive green_eggs_and_ham

  • @Jeffersonquesado ?

  • I put the context of these comments in the chat so as not to pollute here: https://chat.stackexchange.com/transcript/message/40539336#40539336

  • 1

    https://math.stackexchange.com/questions/65137/question-about-the-cat-in-the-hat-problem

  • Thank you guys, you helped a lot! I didn’t even know where to start, sorry for the lack of clarity in the doubt I raised, thank you very much!

  • @Daniloantônio If this answer helped you and clarified your doubts, click the " " next to it, at the top to mark it as accepted/correct, which also marks your question as solved/answered. If you still have questions to clarify, feel free to comment.

Show 3 more comments

Browser other questions tagged

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