Tag: 递归

如果没有return语句,这个C int函数如何工作?

我有这个C代码,我确信它不会起作用,但确实如此。 #include int* find (int* a, int val) { if (*a == val) return a; else find(a+1, val); } int main() { int a[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; int *b; b = find(a, 7); printf(“%d\n”, *b); return 0; } 当然,我从gcc收到警告,因为它在find函数的else分支中缺少一个return语句。 但是,它完美地运作。 为什么会这样? 如何通过递归函数返回int? 当然,最后一次调用返回一个int,但我在void上下文中调用它。

使用分而治之的方法的矩阵乘法

我是编程的初学者,刚刚学习了新的概念并开始编写矩阵乘法的代码,但我在指针和其他方面感到困惑所以我在这里上传代码以寻求指导。 #include #include int **matrixMultiply(int A[][8], int B[][8], int row); int main() { int **A = allocate_matrix(A, 8, 8); int **B = allocate_matrix(B, 8, 8); int i, j; for (i = 0; i < 8; i++) { for (j = 0; j < 8; j++) { A[i][j] = i + j; A[i][j] = i + […]

仅使用一个辅助递归函数查找最长增长子序列的长度

我需要仅使用一个递归函数找到最长单调递增子序列的长度。 例如,给定一个arr={45 1 21 3 33 6 53 9 18}它需要回馈5.我已经开始编写代码但我被卡住了,我不知道如何找出哪个调用给出了最大长度。 函数longestSet是我的辅助函数我可以使用我想要的任何变量,但它必须从函数max_set 。 void question3(int question) { int *arr, size; printf(“enter the array size\n”); scanf(“%d”, &size); arr=(int*)malloc(size*sizeof(int)); fillArr(arr, size-1); max_set(arr, size); free(arr); } void max_set(int arr[], int size) { int i=0, finelmax=0, count=0,longrising; longrising=longestSet(arr,size,i,finelmax,count); printf(“the length of the longest risind set is: %d”, longrising); } int […]

在无向图中查找从一个节点到另一个节点的所有路径

所以我有一个邻接列表forms的图形,结构如下面的代码所示。 鉴于这种forms的数据结构,我想知道如何找到并打印从给定节点到另一个节点的所有可能路径。 我知道我可能不得不使用堆栈来执行DFS或队列来执行BFS,我知道该怎么做,但我对如何找到所有可能的路径感到困惑 typedef struct graph { int n, maxn; Vertex** vertices; }Graph; typedef struct vertex { char* label; Edge* first_edge; }Vertex; typedef struct edge { int u, v; int weight; Edge* next_edge; }Edge ;

递归函数+链表。 sprintf没有将变量保存到struct变量中。

以下代码允许我以递归方式读取目录,并使用链接列表在struct变量中打印所有文件及其路径。 当我打印出正确显示它的完整路径时,我遇到的问题是在函数内,但是当我通过链接列表读取时,完整路径显示为null。 我哪里错了? 代码: #include #include #include #include const char *path_format = “%s/%s”; typedef struct search search; struct search { char *path; char *fileName; char *fullPathToFile; search *next; }; // Modified to take a node ptr. This should be the last node in the list // Returns a node ptr. This is the new last […]

C树XML序列化

我正在尝试递归循环遍历树结构并使用(语言)C将其序列化为字符串。当涉及到C(来自Java,C#,动作脚本背景)时,我是一个真正的新手。一般来说,我很难掌握一些东西。 我应该使用库来帮助生成XML吗? 如何使用C实现递归? 谢谢

如何递归浏览文件夹并计算总文件大小

我试图递归遍历我的目录并打印文件大小,然后在最后打印所有文件大小的总和。 我无法弄清楚以递归方式传递我的函数的内容,并且我的变量总数不会最终正确,任何帮助都非常感谢,非常感谢提前。 #include #include #include #include #include #include void do_ls(char[]); int total = 0; int main(int ac, char *av[]) { if (ac == 1) do_ls(“.”); else { while (–ac) { printf(“%s:\n”, *++av); do_ls(*av); } } } void do_ls(char dirname[]) { DIR *dir_ptr; struct dirent *direntp; struct stat info; if ((dir_ptr = opendir(dirname)) == NULL) fprintf(stderr, […]

使树水平增长,应用对当前节点的修改

给定一个输入,我正在尝试构建一个树,该树应该水平增长,将变换应用于该输入和随后的子节点。 例如,给定输入’aab’和两个转换规则,如: ab -> bba b -> ba 需要构建这样的树: 我已经编写了代码,但是我已经完成了它,我的树垂直工作,我不想那样做。 我需要它水平工作,我不知道我将在何处/如何编写递归。 这就是我现在所拥有的: #include #include #include #include typedef struct t_string_node { struct t_string_node *next; char *value; } string_node; typedef struct t_transformation_rule { struct t_transformation_rule *next; char *needle; char *replacement; } transformation_rule; void findTransformations(char *origin, string_node **transformations, char *needle, char *replacement) { char *str = origin; for […]

二叉树递归函数

我需要打印出一个如下所示的二叉树: ——–x——- —-x——-x— –x—x—x—x- -xxxxxxxx xxxxxxxxxxxxxxxx 使用递归打印行的左侧和行的右侧,但第一行除外。 因此该函数将调用具有左起点和右终点参数的显示函数。 然后它会自动调用两次,左侧为一侧,右侧为一侧。 #include #define LENGTH 16 void makeBranches(int, int); void display(int, int); int main(){ makeBranches(0, LENGTH-1); } void makeBranches(int left, int right){ if(left >= right){ return; } else{ display(left, right); makeBranches(left, (right+left)/2); makeBranches((right+left)/2+1, right); } } void display(int left, int right){ int mid = (left+right)/2; int i; for(i […]

如何在c中为以下要求创建递归目录?

我希望有超过一百万个具有唯一名称的文件。 有人告诉我,如果我将所有这些文件放在一个或两个目录中,这些文件的搜索速度将非常慢。 所以我想出了以下目录架构。 我希望目录结构分支出10个子目录,子目录的级别将是4.因为文件名保证是唯一的我想使用这些文件名来制作可用于放置文件的哈希值在一个目录中,以后也可以找到它。 随机散列值将使目录具有大约1,000个文件。 因此,如果F是根目录,则插入或搜索文件将必须执行以下步骤: 我想使用0-9之间的数字作为目录名称 h=hash(filename) sprintf(filepath,”f//%d//%d//%d//%d//.txt”,h%10,h%10,h%10,h%10); 我如何创建这些目录? 编辑: 所有文件都是文本文件。 该计划将分发给许多人,以收集研究信息。 因此,重要的是这些文件是这样创建的。 编辑: 我创建了以下代码来实现perreal的伪代码。 它编译成功但会给出最后给出的运行时错误。 sprintf()行发生错误。 #include #include #include void make_dir(int depth, char *dir) { if (depth < 4) { if (! CreateDirectoryA (dir,NULL)) for (int i = 0; i < 10; i++) { sprintf(dir,"\\%d",i); char *sdir=NULL ; strcpy(sdir,dir); CreateDirectoryA(sdir,NULL); make_dir(depth + 1, […]