Program with Recursion in C

Asked

Viewed 87 times

1

I need to make a program that is a game, where the user will input a number and I must determine if it is possible for the user to win the game through a printf saying "Yes" or "No", it is not necessary to show the steps of the program. The game is based on the following rule:

*If the number is even half the number is removed;

*If the number is multiplied by 3 or 4, multiply the last 2 digits of the number and subtract from its total;

*If the number is multiples of 5 subtracts 42 from it;

The goal of the game is to finish with the number 42 if you don’t lose. Follow an example below:

1-You start with 250.

2-Like 250 is divisible by 5, you can take out 42, leaving as rest 208.

3-As 208 is even, you can take out half of the total, leaving 104 as rest.

4-As 104 is even, you can take half of the total, leaving 52 as rest.

5-Since 52 is divisible by 4, you can multiply the last two digites of 52 (resulting in 10) and delete 10. This leaves you with 42 left.

Ready! The goal has been achieved! Note, however, that in both step 2 and step 5, you could have used the n even rule. In both cases, you would lose.

for the time being I have the following code:

#include<stdio.h>

int teste1(int num);

int main(){
int num;
int finalNum;
int dado;

scanf("%d",&num);
if(num>=42){

finalNum = teste1(num);
if(finalNum != 42){
        }
 }
 printf("%d",finalNum);
 }

int teste1(int num){
int newNum;
int once;
int i;
int count;

count = 0;

once = 0;

if(once == 0){
    newNum = num;
    printf("%d,%d\n",newNum,num);
    once = 1;
}
if(newNum >=42){

if(newNum % 2 == 0){
    newNum = teste1(newNum-(newNum/2));
}
else if(((newNum % 3 == 0) || (newNum % 4 == 0)) &&  ((newNum%100)/10)*(newNum%10) > 0){
        newNum = teste1(newNum - ((newNum%100)/10)*(newNum%10));
        int dado1 = ((newNum%100)/10)*(newNum%10);
        /*450*/     
}
else if(newNum % 5 == 0){
    newNum = teste1(newNum - 42);
}
}

return (newNum);
}

my problem for now is that my code first makes the check if the number is even then if by chance the number is even and multiples of 5 the program will only do the even number check so in the case of the above example my program will not be able to say that it is possible to win. The only way I thought to fix this problem is to run "void Test1" several times, but changing which check I should do first, but since I can’t use any kind of repeating structure in that code I couldn’t think of any smart way to solve the problem other than creating several void with different check seconds. Any idea?

1 answer

1

This answer is not intended to solve the problems, but rather to show a way to solution.

The idea would be to create four independent functions. Each one returns whether it won or not.

First function (F1)

  • Checks if the number is equal to 42, if yes returns won.
  • If the number is smaller q 42 returns no win.
  • Otherwise returns F2 or F3 or F4. If any function returns that wins then returns.

F2 - If the number is even half of the number is removed;

F3 - If the number is multiplied by 3 or 4, multiply the last 2 digits of the number and subtract from its total;

F4 - If the number is multiplied by 5, 42 are subtracted from it;

For the implementation of this (F2,F3,F4) do the following:

  • If the condition is true then perform the condition and this new number returns F1 of this.
  • Otherwise return did not win.

Browser other questions tagged

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