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
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.
#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?
546 Understanding How to Configure Google API Key
703 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
547 rails db:create error: Could not find mysql2-0.5.4 in any of the sources
549 PHP ssh2_scp_send fails to send files as intended
547 Who developed the "avformat-59.dll" that comes with FFmpeg?
© 2024 OneMinuteCode. All rights reserved.