Tag: 递归

使用C中的递归函数测试回文结构

我尝试编写用于测试字符串的程序,如果它是一个回文或不是,但我总是得到输出,因为它不是一个。 我的代码出了什么问题? #include #include int is_palindrome(int start, int end, char *str) { if (str[start] != str[end]) return 0; else if (start == end) return 1; else return is_palindrome(++start, –end, str); return 0; } int main() { char str[20]; int length,start=0,end=length-1; int result; printf(“Enter the String.\n”); fgets( str, sizeof( str ), stdin ); length = strlen(str); […]

堆栈内存使用算术和递归函数调用

我关心的是在涉及算术和递归函数调用的指令中将使用堆栈内存,例如: return 5 + f(a-1); //f is recursive 我想了解什么放在堆栈上,什么时候,以便我知道在深度递归的情况下什么可能或不可能导致内存问题。 这是一个更完整的例子: int f(int a) { if (a > 0) { return 5 + f(a-1); } else { return 0; } } 让我们关注线路return 5 + f(a-1); 在这一点上我们将在内存中存储什么? 我们在堆栈中有一个整数a的位置,因为变量是f的本地变量 是否会存储值5和1? 操作a-1的结果怎么样,它会被放到堆栈上? f的返回值怎么样? 关于所使用的内存量的“最坏情况”场景将是在某些时候所有这些将同时在堆栈上。 更好的情况是,将按顺序分配和解除分配,以便不是所有内容都保存在内存中。 怎么会发生? 是由编译器决定的吗? 非常感谢,

通过使用递归确定两个数组是否相互置换

我在编写一个代码时遇到了一些困难,该代码通过使用递归来确定两个未排序的数组是否是彼此的排列。 我知道如何通过非递归代码确定它,使用排序 – 但我不知道如何通过使用递归来做到这一点。 到目前为止,我无法得到任何真正的想法…… int CheckPermutation(int arr1[], int arr2[], int size) { if (size == 0) return 1; if (size == 1) return (arr1[0] > arr2[0]); } 这就是我所尝试的,我发现很难从那一点继续

调用函数时char数组和char *数组之间的区别?

我想知道为什么这个代码与char tab[100]工作正常,但如果我使用char *tab不起作用? fgets函数将char*数组作为参数对吗? #include #include #include Int Palindrome(char* str, int i, int j); int main() { char tab[100]; printf(“Enter your string : \n”); fgets(tab, 100, stdin); int j = strlen(tab); printf(“%d\n”, Palindrome(tab, 0, j – 2)); return 0; } int Palindrome(char* str, int i, int j) { if (i >= j) { printf(“My word […]

C中的递归不返回字符串值

我正在学习C并且这样做我决定写一个数独求解器。 我无法使用solve函数返回已解决的板,我的想法是问题是递归函数调用。 我将板作为字符串传递,找到板中第一个“0”的索引,并使用该索引构建该位置的可能值列表。 然后我迭代可能性,复制原始板,并用可能性替换零,然后递归地将新板传递给solve函数。 代码如下: char *solve(char *board) { int zero = strcspn(board, “0”); if(zero > 80) { return board; } else { char *possibilities = getPossibilities(zero, board); if(possibilities != ‘\0’) { for(int i = 0; i < strlen(possibilities); i++) { char *new_string = malloc(strlen(board) * sizeof(char)); memcpy(new_string, board, strlen(board)); new_string[zero] = possibilities[i]; return solve(new_string); […]

无法在C中实现递归调用

所以我第一次和C一起工作,我觉得我在使用递归时遇到了麻烦。 例如,要在C#中return methodRecursiveCall(parameter)递归调用的值,我可能会使用return methodRecursiveCall(parameter) 。 在C中,我有这个声明,它是罗马数字转换器的一部分: int convert(char *s) { int val = 0; int index = 0; int length = strlen(s); while (length >1) { if (s[index] == ‘I’) { if(s[index + 1] == ‘V’ || s[index + 1] == ‘X’ || s[index + 1] == ‘C’ || s[index + 1] == ‘D’ || […]

C中的递归和链表

我正在创建一个函数,它接受一个路径并读取其中的所有文件并创建一个链接列表。 读取目录效果很好,但是我很难在链表中创建和存储相关信息以供日后使用。 这是我目前正在使用的结构: typedef struct searchPool searchPool; struct searchPool{ char * path; char * fileName; char *pathFile; searchPool * next; }; 创建“SearchPool”类型的新元素的函数定义如下: searchPool * mallocStructPool (char * path, char * fileName, char * filePath ) { searchPool * element = (searchPool*)malloc(sizeof(searchPool)); element->path = malloc(sizeof(char * )); element->fileName = malloc(sizeof(char * )); element->pathFile = malloc(sizeof(char * […]

理解C中的递归

这是一个递归程序。 但我不明白在这个项目中发生的事件顺序 #include count(int); main() { int x=5; count(x); } count(int y) { if(y>0) { count(–y); printf(“%d “,y); } } 输出是: 4 3 2 1 0 … 但是我没有得到第一次调用count(5)和调用count(4)时会发生什么。 控件是否立即进入函数的开头? 或者首先它打印y的值然后再次进入函数count()的开头?

理解递归十进制到二进制代码?

我正在开发一个可以将数字转换为二进制forms的程序。 在帮助下,我能够得到它,它似乎工作,但我只是不明白如何。 我想最好的方法是尝试解释我认为这是如何工作的,有人可以纠正我。 我有一个函数,它有一个if语句,表示如果n除以2不等于0则将n除以2.然后如果n / 2则为1或0,则打印余数。 main函数只运行我给它的任何数字的函数,在本例中为456。 但程序如何知道多次运行该函数以获得整个二进制forms? 我觉得这并不复杂,但我没有得到它。 #include void ConvertToBinary(int n) { if (n / 2 != 0) { ConvertToBinary(n / 2); } printf(“%d”, n % 2); } int main (){ ConvertToBinary (456); return 0; }

最长增加子序列中的荒谬条件

/* A Naive recursive implementation of LIS problem */ #include #include /* To make use of recursive calls, this function must return two things: 1) Length of LIS ending with element arr[n-1]. We use max_ending_here for this purpose 2) Overall maximum as the LIS may end with an element before arr[n-1] max_ref is used this […]