Tag: 递归

NxN矩阵行列式递归问题

我目前正在尝试编写一个程序来找到NxN矩阵的行列式,但我有一个N大于2的递归问题。基本上我可以告诉它,它没有这样做,它只是运行一次使用函数我的调试选项显示该函数在列中运行,但顺序永远不会下降,然后无论如何,它都会为我的行列式提供零。 香港专业教育学院曾试图寻找任何想法,因为我做错了什么,但我似乎找不到任何答案,我甚至找到了与我做基本相同的事情的例子,使用它们给我零,不管怎么样,所以我非常困惑:(。如果有人可以快速查看我的代码并告诉我哪里是一个白痴,我会非常感激!(抱歉格式化,它在我的编辑器中看起来不错但我似乎无法得到它在这里) 码: #include #include #include double det(double **mat, int order); int main (int argc, char* argv[]) { FILE* input; int row,column,N; double **matrix; N=3; matrix=(double**)malloc(N*sizeof(double)); input=fopen(“matrix.dat”, “r”); if(input !=(FILE*) NULL) { for(row=0; row<N; row++) { matrix[row]=(double*)malloc(N*sizeof(double)); } for(row=0; row<N; row++) { printf("| "); for(column=0; column<N; column++) { fscanf(input,"%lf ", &matrix[row][column]); printf("%g ", matrix[row][column]); } […]

C中的递归因子程序在执行时挂起

我正在编写一个程序来显示计算给定数量200万次的阶乘所需的时间。 我是在C / C ++ Eclipse环境中使用Debian Linux编写的。 当程序到达int temp = n * rfact(n-1); ,它挂起,不会做任何其他事情。 这是我到目前为止所得到的: #include #include //prototypes int rfact(int n); main() { int n = 0; int i = 0; double result = 0.0; clock_t t; printf(“Enter a value for n: “); scanf(“%i”, &n); printf(“n=%i\n”, n); //get current time t = clock(); //process factorial […]

C中函数的返回值

int ret(char *) { //nothing } int main(void) { printf(“%d\n”,ret(“foo”)); } 为什么函数ret返回一个垃圾值??? 和 int my_strlen(char *s) { if(*s) return 1 + my_strlen(s + 1); // or my_strlen(s+1) + 1; } 在上面的函数中,如果我没有错,my_strlen应该总是在条件失败时(即每当它到达’\ 0’时)返回垃圾值,因为没有其他情况或任何返回语句的错误情况。 所以对于任何有效的字符串,它应该总是返回1 +垃圾值。 但事实并非如此。 它工作正常。 它给出了任何字符串的精确长度。 为什么它不像“ ret ”函数那样返回垃圾值? 请清楚我的怀疑。 任何帮助将不胜感激。 感谢名单..

Tricky Segmentation在C中的BST递归出错

我正在尝试使用递归插入方法(通常用于BST,IIRC)将字符串添加到二进制搜索树中,以便稍后我也可以使用递归将它们打印出来。 麻烦的是,我一直在得到一个我不太懂的分段错误。 相关代码如下(这段代码来自我的主函数): #include #include #include #include // Stores the size of the C-strings we will use; // Standardized to 100 (assignment specifications say // ALL strings will be no more than 100 characters long) // Please note that I defined this as a preprocessor // directive because using the const keyword makes it // […]

递归和斐波那契序列

如何使用此代码打印给定术语的斐波那契序列的所有值? 现在它只打印最后一个术语 #include int fibonacci(int n){ if (n==2) return 1; else return fibonacci(n-1) + fibonacci(n-2); } int main() { int n; int answer; printf(“Enter the number of terms you’d like in the sequence\n”); scanf(“%d”,&n); answer = fibonacci(n); printf(“The answer is %d\n”, answer); }

在C中使用递归的数字总和

对于我们今天的活动,我们的任务是使用数字之和进行递归。 我已经制作了这个节目: int main() { int num = 0, sum; printf(“Enter an integer: “); scanf(“%d”,&num); //counter=1; for ( sum=0; num>0;) { sum = sum + num % 10; num = num /10; } printf(“Sum = %d”, sum); getch(); return 0; } 我们的老师补充说“输入和输出必须在main()函数中完成。” 我做对了吗? 或者我在代码中遗漏了什么?

C – 使用后序遍历释放二叉树的内存

我想使用后序遍历删除二叉树 。 这意味着应首先删除树的左侧部分, 然后删除右侧的树,然后在后面的第二个函数中删除整个树并释放内存 。 我不允许更改函数的参数,只能使用它的内部: #include #include #include #include “telefonbuch.h” static inline bstree * create_node(unsigned long phone, char * name) { bstree * newNode = (bstree *) malloc(sizeof(bstree)); newNode->key.phone = phone; strcpy(newNode->key.name, name); newNode->left = NULL; newNode->right = NULL; return newNode; } void bst_insert_node(bstree * bst, unsigned long phone, char * name) { if […]

创建递归二叉树?

我有两个堆栈,一个用操作数,另一个用操作符。 我的问题是将这两个堆栈变成二叉树。 例如,表达式(2+3)*(4-3)将被转换为后缀(例如24+43-* ),然后放入两个堆栈3442和*-+将成为堆栈(顶部为分别为3和*)。 现在使用这些堆栈,我需要形成一个像二进制树 * + – 2 3 4 3 有没有办法递归地做到这一点? 现在,我有一个像这样的算法: 创建树的根,将根的值分配给operator-stack中的第一个运算符。 将右指针和左指针设置为null。 创建正确的节点,如果存在,则分配下一个运算符的值,如果不为其分配操作数。 然后对左节点执行相同操作。 我的问题是使这个递归,或让它来处理许多不同的情况。 谢谢你的帮助。

如何系统地跟踪递归?

好吧,我有许多’C语言’测试涉及查找给定函数的输出,而且,我需要准确解释它的目的是什么。 其中一些是递归函数。 当我遇到递归时,我一直在努力寻找如何示意地遵循它 ,即使我成功了,有时我也可能不明白递归函数的目的是什么。 这是2段代码: 主要 #include #include int f2(int *a, int n, int x); int main() { int a[6] = {4, 3, 4, 2}; printf(“%d\n”, f2(a, 4, 5)); getch(); } f2function: int f2(int *a, int n, int x) { if(n>0 && x!=0){ return (f2(a+1,n-1,xa[0]) ? 1 : f2(a+1,n-1,x)); } return (x ? 0 : […]

99瓶啤酒递归似乎不起作用

好的,这是我在学习过程中编写的简单代码。 void SingTheSong (int NumOfBottles) { if (NumOfBottles == 0){ printf(“there are simply no more bottles of beer on the wall. \n”); } else { printf(“%d bottles of beer on the wall, %d bottles of beer.\n”, NumOfBottles, NumOfBottles); int Bottleless = NumOfBottles – 1; printf(“Take one down pass it around, %d bottles of beer on […]