Program with C Recursion

0

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", not it is necessary to show the steps of the program. The game is based on the following rules:

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

* If the number is multiples of 3 or 4 multiplies the last 2 digits of the number and subtracts from its total;

* If the number is multiplied by 5, subtract 42 from it;

The goal of the game is to end with the number 42 if not you lose. Here's an example below:

1-You start with 250.

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

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

4-Like 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 digits by 52 (resulting in 10) and delete 10. That leaves you with 42.

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

For now 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 checks whether the number is even so if by chance the number is even and multiple of 5 the program will just do the even number check then in the above example my program will not be able to say that it is possible to win. The only way I thought of fixing this problem is to run "void test1" several times, however changing what check I should do first, but since I can not use any kind of repeat structure in that code I could not think of any clever way to solve it problem other than creating multiple void with different check strings. Any ideas?

    
asked by anonymous 19.11.2017 / 20:43

1 answer

0

This answer is not intended to solve problems, but to show a way to solve them.

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

First function (f1)

  • Checks whether the number is equal to 42, if yes returns won.
  • If the number is less than 42 returns it did not win.
  • Otherwise returns f2 or f3 or f4. If any function returns that won then returns won.

f2 - If the number is even, half the number is removed;

f3 - If the number is multiples of 3 or 4 multiplies the last 2 digits of the number and subtracts from its total;

f4 - If the number is multiple of 5 subtract 42 from it;

For the implementation of this (f2, f3, f4) do the following:

  • If the condition is true then it performs the condition and this new number returns f1 of this.
  • Otherwise returns will not win.
20.11.2017 / 01:45