Tag: 递归

Bytelandian Gold Coin,动态编程,解释?

这有点不成熟,但我不得不问, 这里提到的Bytelandian金币问题 – http://www.codechef.com/problems/COINS/ ,据说是典型的DP问题,尽管我已经阅读了DP和递归的基础知识,但我发现很难理解它解, # include # include long unsigned int costArray[30][19]; unsigned int amount; unsigned int currentValue(short int factor2,short int factor3) { int j; unsigned int current = amount >> factor2; for(j=0;j<factor3;j++) current /= 3; return current; } long unsigned int findOptimalAmount(short int factor2,short int factor3) { unsigned int n = currentValue(factor2,factor3); if(n […]

生成数字的所有不同分区

我正在尝试编写一个C代码来生成具有给定数字的不同元素的所有可能的分区(分成2个或更多部分)。 给定分区的所有数字的总和应该等于给定的数字。 例如,对于输入n = 6 ,具有2个或更多具有不同元素的元素的所有可能分区是: 1,5 1,2,3 2,4 我认为递归方法应该有效,但我无法处理不同元素的附加约束。 非常感谢C / C ++ / Java中的伪代码或示例代码。 谢谢! 编辑:如果它使事情变得更容易,我可以忽略具有至少2个元素的分区的限制。 这将允许将数字本身添加到列表中(例如,6本身将是一个微不足道但有效的分区)。

将迭代函数转换为递归函数

我知道人们通常会反过来问这个问题,但我有以下问题:我有这个迭代函数,它计算包含数据值20的循环双链表中的所有节点。现在,我如何使这个递归,递归函数的基本情况(终止案例)是什么? 任何帮助表示赞赏: int count(node *start) { int c; c = 0; if(start == NULL) return 0; if((start->roll_no) == 20) c = 1; node *current = start; while(current->next != start) { if((current->next->roll_no) == 20){ c++; } current = current->next; } return c; }

检查C中的字符串是否是回文结构

我有一个关于这个代码的问题,我正在写一个练习。 我要检查字符串是否是回文。 我无法更改函数的声明。函数只返回1,当所有字母都相同(如“aaaa”)但如果我用其他回文(如“anna”)收取句子,函数返回0,我无法弄清楚为什么会出现这种情况。谢谢! char* cargar (char*); int pali (char*); int main() { char*texto=NULL; texto=cargar(texto); int res=pali(texto); if(res==1){printf(“\nPalindrome”);} else printf(“\nNot palindrome”); return 0; } char* cargar (char*texto) { char letra; int i=0; texto=malloc(sizeof(char)); letra=getche(); *(texto+i)=letra; while(letra!=’\r’){ i++; texto=realloc(texto,(i+1)*sizeof(char)); letra=getche(); *(texto+i)=letra;} *(texto+i)=’\0′; return texto; } int pali (char* texto) { int i; for(i=0;*(texto+i)!=’\0′;i++){ }i–; if(i==0||i==1){return 1;} if(*texto==*(texto+i)){ […]

解释所需的C递归

我需要一些特殊代码的帮助。 我并没有真正理解它。 有人可以花点时间向我解释一下吗? 它所做的就是用一个字来打印出它的反面。 我不理解递归部分,但是对于n = 1,for循环只运行一次,递归将运行直到它读取整个单词并且它符合”标记,但是它如何打印出反转字? void reverse() { char c; scanf(“%c”, &c); if (c!=’ ‘) { reverse(); } printf(“%c”, c); } int main() { int n, i; printf(“\nThe number of the words=”); scanf(“%d”, &n); for(i=1; i<=n; ++i) { reverse(); printf("\n"); } printf("\nEnd of the program.\n"); return 0; }

了解啤酒瓶示例中的递归

我自己在C中练习递归,我在网上找到了这个例子。 但有一件事我不明白。 void singSongFor(int numberOfBottles) { if (numberOfBottles == 0) { printf(“There are simply no more bottles of beer on the wall.\n\n”); } else { printf(“%d bottles of beer on the wall. %d bottles of beer.\n”, numberOfBottles, numberOfBottles); int oneFewer = numberOfBottles – 1; printf(“Take one down, pass it around, %d bottles of beer on […]

C中的尾递归

我试图编写递归函数,找到一个数的阶乘。 int factorial(int input,int *answer) { if ( input ==0 ) { return 0; } *answer = *answer * input; factorial(input -1, answer); } 你会对这个function说些什么? 是尾递归吗?

以递归方式查找数组的最大元素

考虑这段代码,它计算数组的最大元素。 #include int maximum(int arr[], int n) { if (n == 1) { return arr[0]; } else { int max = maximum(arr, n-1); printf(“Largest element : %d\n”, max); return 5; // return arr[n-1] > max ? arr[n-1] : max; } } int main() { int array[5] = {5, 23, 28, 7, 1}; printf(“Maximum element of […]

究竟什么是递归并解释这个程序的输出?

我真的无法理解这段代码。 当一个函数调用自己真正发生的事情时? 我知道它与堆栈的概念有关,但我仍然无法解决这些问题。 #include fun(int); main() { int x=3; fun(x); } fun(int a) { if(a<0) { fun(–a); // what happens when function calls itself printf("%d",a); fun(–a); } } 请解释在此过程中发生的事件顺序。

如何在迷宫中找到最快的路线(在C中)

迷宫被定义为方阵。 例如: int maze[N][N] = { { 1, 1, 1, 1, 1, 1, 1 }, { 0, 1, 0, 1, 0, 0, 1 }, { 0, 1, 0, 1, 1, 1, 1 }, { 0, 1, 0, 0, 0, 1, 1 }, { 0, 1, 1, 1, 0, 1, 1 }, { 0, 0, 1, […]