Please help me with the language recursive function problem!!!

Asked 2 years ago, Updated 2 years ago, 51 views

Farmer John developed a new way to line up the cows for dinner. N (1-15) cows should be placed in order, and a napkin with one of the three +, -, and . written between each cow should be placed so that the final result is zero. A larger number can be made through a napkin with dots (.). Let's take a look at the case below. (The napkin with p.s. written on it can be thought of as a 'blank').)

1-2.3-4.5+6.7

Such arrangements represent 1-23-45+67. The result is zero. 10.11 is interpreted as 1011.

Input

In the first line, the number N of cows is entered.

Output

It outputs 20 possible answers for the first 20 lines, which are preceded in dictionary order. The order is preceded by + and followed by - and . If there are fewer than 20 answers, output the possible answers with a space between each number and letter. Print them all out. The last line outputs the total number of possible answers.

Enter example

7

Example Output

1 + 2 - 3 + 4 - 5 - 6 + 7

1 + 2 - 3 - 4 + 5 + 6 - 7

1 - 2 + 3 + 4 - 5 + 6 - 7

1 - 2 - 3 - 4 - 5 + 6 + 7

1 - 2 . 3 + 4 + 5 + 6 + 7

1 - 2 . 3 - 4 . 5 + 6 . 7

6

Please solve this problem with C~~!

c

2022-09-22 18:45

2 Answers

You have to do your own homework.

The questioner also pays a lot of money to learn, so try it and ask questions that don't work out.


2022-09-22 18:45

#include <stdio.h>
#define enoughNumber 100

static char str[enoughNumber]; 

int calc(
    int myStrLocation,  int mySign, 
    int myNum,      int current_calc_value, 
    int stack,      int limit = 15)
{
    switch(mySign){
        case 1: // +
            str[myStrLocation]='+';
            myStrLocation++;
        case 2: // -
            str[myStrLocation]='-';
            myStrLocation++;
        case 3: // .
            // // nothing
    }
    if(myNum<10){str[myStrLoaction]=myNum;myStrLoaction++;}
    else{
        str[myStrLoaction]=1;myStrLoaction++;
        str[myStrLoaction]=myNum%10;myStrLoaction++;        
    }

    if(mySign==2){stack=-1*myNum;}

    if(myNum==limit){
        for(;myStrLocation<enoughNumber;myStrLocation++){
            str[myStrLocation]=0;
        }

        if (Current_calc_value+stack ==0){
            printf("%s",str);

        }
    }
    else{
        calc(myStrLocation, 1, myNum+1, Current_calc_value+stack, 0, limit);
        calc(myStrLocation, 2, myNum+1, Current_calc_value+stack, 0, limit);
        calc(myStrLocation, 3, myNum+1, Current_calc_value, stack*10, limit);
    }

}

int main(){

    printf("Enter a small number: ");
    int Num=0;
    scanf("%d",&Num);
    calc(0,1,0,0,0, Num);

return 0;}

Will it work?


2022-09-22 18:45

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.