我有一個問題,如何列印這個問題的結果。
我設計了遞回函式來解決,因為它是必需的條件,但真的不知道如何列印結果
這是問題
person1 和 person2 玩“用數字表達目標”的游戲。他們通過使用輸入字串,數字運算式來制作目標數字: , -, *。
如果他們成功制定目標,他們將通過數值運算式列印如何制定目標編號。如果沒有,列印"None"
首先,他們輸入字串編號,如: "123"
第二個輸入目標如: "6"
我們只能使用數值運算式: ' ', '-', '*'
制作目標的方式:6 by"123"是"1 2 3", "1 * 2 * 3"
制作目標的方法:7 by"125"是"1 * 2 5", "12 - 5"
如果我們不能通過輸入字串編號來制作目標,列印"None"
例如) input string: "123", target: "4"=> 沒有結果可以制作目標。
#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);
}
}
首先,我認為我可以列印結果以將數字保存在陣列(或堆疊)中,但我無法通過我的想法。
uj5u.com熱心網友回復:
我認為這段代碼做了你想要做的。請注意,它的效率不是很高,但我猜這不是問題。首先,它使用遞回函式將數字和運算子的所有組合創建為字串,并對其進行評估以檢查它是否等于所需的值。由于看起來像你的大學作業,我就不詳細解釋了,盡量自己去了解細節。
示例輸出:
Target=22, string=123456
found: 1 2*3 4 5 6
found: 1-2 3*4 5 6
found: 1-2 34-5-6
found: 1-2-3 4*5 6
found: 1-2-3-4 5*6
found: 12 3-4 5 6
found: 12*3-4*5 6
#include <stdio.h>
#include <math.h>
#include <string.h>
#define max_string_num 11
bool found=false;
void find_target(const char string[], const char result[], const int target, const int end, const int size);
//Evaluate expression, str=string to evaluate, len - length of string, sign - sign
int eval(const char* str, const int len, const int sign=1)
{
for (int i = 0; i < len; i ){
if (str[i] == ' ') return sign*eval(str, i) eval(str i 1, len-i-1);
if (str[i] == '-') return sign*eval(str, i) eval(str i 1, len-i-1,-1);
}
for (int i = 0; i < len; i )
if (str[i] == '*') return sign*eval(str, i) * eval(str i 1, len-i-1);
char tmp[max_string_num];
strncpy (tmp, str, len );
tmp[len] = '\0'; //copy a zero at the end
return sign*atoi(tmp);
}
void process_operator(const char op, const char string[], const char result[], const int target, const int end, const int size)
{
char str1[max_string_num]; //string of partitioning number 1
char str2[max_string_num]; //string of partitioning number 2
// partitioning strings
strncpy (str1, string, size );
str1[size] = '\0'; //copy a zero at the end
strncpy (str2, string size, end-size);
str2[end-size] = '\0';
char tmp[2*max_string_num];
sprintf(tmp,"%s%s%c%s", result, str1,op, str2);
if (eval(tmp, strlen(tmp)) == target) {
found=1;
printf("found: %s\n", tmp);
}
for(int i = 1; i < end-size; i )
{
sprintf(tmp,"%s%s%c",result,str1,op);
find_target(string size, tmp, target, end-size, i);
}
}
void find_target(const char string[], const char result[], const int target, const int end, const int size) {
process_operator(' ',string, result, target, end, size);
process_operator('-',string, result, target, end, size);
process_operator('*',string, result, target, end, size);
}
int main() {
char result[]="";
char string[max_string_num]="123456";
int target=12*3-4*5 6;
printf("Target=%d, string=%s \n", target, string);
int 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, result, target, len, i);
}
if(!found) printf("None");
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/318699.html
