标签: recursion

使用 ktolin 上的递归进行阶乘的 Stackoverflow 错误

这是我的代码 这在输出控制台上给出了 30 次堆栈溢出错误

fun main(args:Array<String>){
     var no:Int=Integer.parseInt(readLine())//read input from user and convert to Integer
      var ans:Int=calculateFact(no) //call function and store to ans variable
      println("Factorial of "+no+" is "+ans) //print result
}
fun calculateFact(no:Int):Int //function for recursion
{
if(no==0) {
    return 1 }
return (no*calculateFact(no))   
}
Run Code Online (Sandbox Code Playgroud)

我不知道什么是错误解决plz

recursion factorial kotlin

-3
推荐指数
1
解决办法
165
查看次数

将嵌套循环转换为递归

我使用了 3 个嵌套循环。现在我想将这些循环转换为递归。还有将循环转换为递归的通用方法吗?

#include <stdio.h>

#define f(x, y, z) ((x + y) * (y + z))

int main()
{
    int test_case, p, q, r, i, j, k, a[100001], b[100001], c[100001], sum;
    scanf("%d", &test_case);
    while (test_case--) {
        scanf("%d%d%d", &p, &q, &r);
        sum = 0;
        for (i = 0; i < p; i++) {
            scanf("%d", &a[i]);
        }
        for (i = 0; i < q; i++) {
            scanf("%d", &b[i]);
        }
        for (i = 0; i < p; i++) {
            scanf("%d", &c[i]);
        }
        for …
Run Code Online (Sandbox Code Playgroud)

c recursion

-3
推荐指数
1
解决办法
6562
查看次数

python中使用递归插入二叉搜索树

我试图使用递归在二叉搜索树中插入值,但是当我使用中序遍历运行它时,我得到 None 的输出。我尝试查看其他语言实现此功能,我只是尝试复制它,但它不起作用。我将树的根传递给插入函数,如果它不为空,我希望它向左或向右遍历。有人可以告诉我这有什么问题吗?我尝试将 bst.root 转换为 bst.get_root() ,但仍然产生相同的结果。

class Node:
    def __init__(self, value):
        self.value = value
        self.left = None
        self.right = None

class BinaryTree:
    def __init__(self):
        self.root = None
        self.size = 0

    def get_size(self):
        return self.size

    def get_root(self):
        return self.root

    def insert(self, root, value):
        if root is None:
            root = Node(value)
        else:
            if value < root.value:
                root.left = self.insert(root.left, value)
            else:
                root.right = self.insert(root.right, value)
        return root

    def inorder(self, root):
        if root == None:
            return
        else:
            self.inorder(root.left)
            print(root.value, end=" -> ") …
Run Code Online (Sandbox Code Playgroud)

python recursion insert binary-search-tree

-3
推荐指数
1
解决办法
5974
查看次数

文本对齐算法

这是DP中一个非常著名的问题,有人可以帮助可视化它的递归部分吗?排列或组合将如何生成。

问题参考。 https://www.geeksforgeeks.org/dynamic-programming-set-18-word-wrap/

java recursion dynamic-programming

-3
推荐指数
1
解决办法
3429
查看次数

如何查找递归函数中的减法次数

这是代码:

def f(k):
    if k<3:
        return 1
    return f(k-1)+f(k-2)+f(k-3)
Run Code Online (Sandbox Code Playgroud)

提前致谢!

python recursion

-3
推荐指数
1
解决办法
50
查看次数

如何递归地查找max数组元素的索引

我想以递归方式找到数组中最大元素的索引.函数的声明可以是这样的:

int maxIndex(const int *p, int size)
Run Code Online (Sandbox Code Playgroud)

我正在研究递归,我看到了一些例子,比如递归地查找max数组元素.这很简单:

int maxInt( const int * p, int size)   
{

    if(size == 1)
        return *p;

    int max = maxInt(p + 1, size -1 );

    if(max > *p)
        return max;
    else
        return p[0];
}
Run Code Online (Sandbox Code Playgroud)

我问自己,如何找到包含数组最大元素的索引.我甚至不确定它是否有可能.你怎么看?

c c++ arrays recursion

-4
推荐指数
1
解决办法
1907
查看次数

Java-使用递归查找数组中的最大数字

我试图使用递归来查找数组中最大的数字,但没有得到我希望的结果。任何帮助将不胜感激。

public class ArrayMax {
    public static int largestInteger(int[] array) {

        return FindlargestInteger(array, 0, -99999999);

    }   

    public static int FindlargestInteger(int[] array, int index, int max) { 

        if (index == array.length)
            return max;

        if (array[index] > max) {
            max = array[index];
        }

        FindlargestInteger(array, index + 1, max);

        return max;     
    }
}
Run Code Online (Sandbox Code Playgroud)

java arrays recursion

-4
推荐指数
1
解决办法
6778
查看次数

如果我有一个调用其他实例之一的重载函数,它是否被认为是一个递归函数?

这是假设,但我有一个带有两个重载构造函数的类 - 其中没有一个是默认的构造函数.如果我从另一个调用一个构造函数,它会递归吗?例:

class Example
{
     Example(const int integer)
     {
          //Constructor Code Here
     }

     Example(argument)
     {
          Example object(68);
          //Rest of constructor code
     }
};
Run Code Online (Sandbox Code Playgroud)

c++ recursion constructor class function-calls

-4
推荐指数
1
解决办法
97
查看次数

如何找到递归选择排序的时间复杂度?

嘿伙计们,我明天有一个测验,我遇到了一些问题,寻找选择排序的递归程序的时间复杂度,任何人都可以解释它是如何 n^2 的。还有一个问题,一些排序算法循环的时间复杂度为 n/2,对于新手问题,/2 是什么意思。

#include <iostream>
using namespace std;

// recursive function to perform selection sort on subarray arr[i..n-1]
void selectionSort(int arr[], int i, int n)
{
    // find the minimum element in the unsorted subarray[i..n-1]
    // and swap it with arr[i]
    int min = i;
    for (int j = i + 1; j < n; j++)
    {
        // if arr[j] element is less, then it is the new minimum
        if (arr[j] < arr[min])
            min = j;    // update …
Run Code Online (Sandbox Code Playgroud)

c++ algorithm recursion data-structures

-4
推荐指数
1
解决办法
1953
查看次数

std::vector 的结构是什么?

我已经用递归方式打印向量的所有元素,但它返回无意义!它抛出了一个非常奇怪的异常:

Exception thrown: read access violation.
std::vector<int,std::allocator<int> >::operator[](...) returned nullptr.
Run Code Online (Sandbox Code Playgroud)

它输出: 12358000

这是代码。我犯了什么错误?

#include <iostream>
#include <vector>
using namespace std;

int printVec(vector<int>* foo) {
    if ((*foo).empty())
        return 0;
    else {
        cout << (*foo)[0];
        printVec(foo + 4);
    }
}
int main() {
    vector<int> ref{ 1,2,3,4,5,6,7,8,9,0 };
    printVec(&ref);
}
Run Code Online (Sandbox Code Playgroud)

c++ recursion stdvector

-4
推荐指数
1
解决办法
88
查看次数