I have a question how to print results in this problem.
I designed recursive function to solve because it's required condition but really don't know how to print the results
Here is problem
person1 and person2 play the game "target making by numerical expression". They make target number by using input string, numerical expression: +, -, *.
If they succeed to make target, they print how to make target number by numerical expression. If not, print "None"
First they input the string number like: "123"
Second input target like: "6"
We can only use numerical expression: '+', '-', '*'
The way to make target: 6 by "123" is "1 + 2 + 3", "1 * 2 * 3"
the way to make target: 7 by "125" is "1 * 2 + 5", "12 - 5"
If we can't make target by input string number, print "None"
e.g) input string: "123", target: "4" => there is no result to make target.
#include <stdio.h>
#include <math.h>
#include <string.h>
#define max_string_num 11
void find_target(char string[], int target, int start, int end, int size);
int main() {
char string[max_string_num];
int result[100];
int target;
int len, count = 0;
scanf("%s %d", string, &target); //input string number and input target number
len = strlen(string); // check string number length
//partitioning string number by recurtion function
//can change the number of digit by changing 'i'
for(int i = 1; i < len; i++)
find_target(string, target, 0, len, i);
return 0;
}
void find_target(char string[], int target, int start, int end, int size) {
int i;
int num1 = 0, num2 = 0; // save results of partitioning numbers
// partitioning numbers
for (i = start; i < size; i++)
num1 += (string[i] - '0') * pow(10, size - 1 -i);
for (i = size; i < end; i++)
num2 += (string[i] - '0') * pow(10, end - 1 - i);
//finish recursion
if (end == size)
return;
// i don't know how to print result when i find target result
if ((num1 + num2) == target) {
}
else if ((num1 - num2) == target) {
}
else if ((num1 * num2) == target) {
}
else {
//if don't make target by num1, num2, go recursion and partitioning
find_target(string, target - num1, start + 1, end, size + 1);
find_target(string, num1 - target, start + 1, end, size + 1);
find_target(string, target / num1, start + 1, end, size + 1);
}
}
First, I thought that I can print the result to save number in the array (or stack), but I can't make through my idea.