标签: recursion

C递归不会递归

我的搜索没有产生任何结果.另外,我的英语非常糟糕,对不起.

所以我的程序应该这样做:

https://i.imgur.com/Ss8Oa2R.png

#include <stdio.h>
#include <stdlib.h>
#include <locale.h>

float rec();

int main()
{
  setlocale(LC_ALL,"");
  int w;
  scanf("%d",&w);
  printf("%f",rec(w));
  return 0;
}

float rec(n)
{
  if (n<1)
  {
    printf("Error");
  }
  else if (n==1)
  {
    return 6;
  }
  else if (n>1)
  {
    return 1/2*rec(n-1)+4;
  }
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

当输入为1时,它输出6,正如它应该的那样.

当输入<1时,输出"Error0,000000".

当输入> 1时,它只输出添加的数字

return 1/2*rec(n-1)+4;
Run Code Online (Sandbox Code Playgroud)

在这种情况下,它输出4.为什么?rec(n-1)== 0?

我的问题是:

  1. 为什么它不起作用?
  2. 当输入<1时,如何输出"Error"而不是"Error0,000000"?

c recursion function

-2
推荐指数
1
解决办法
81
查看次数

为什么递归函数的输出为0?

有人可以解释一下为什么这段代码返回0?

#include <stdio.h>
int factorial(int input)
{
    if (input > 0)
    {
        input--;
        return input * factorial(input);
    }
    return 1;
}
int main()
{
    printf("%d", factorial(20));

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

c recursion

-2
推荐指数
1
解决办法
54
查看次数

递归地在数组元素处取零值c

我有递归的问题.我希望所有负数都替换为零.我希望递归.但出了点问题,让我失去了价值.有人可以帮帮我吗?

#include <stdio.h>

int zeros_value(int n, int tab[])
{
    if (n==0) return 0;
    if(tab[n-1] < 0){
       tab[n-1]=0;
    }
    else{ 
        return zero_value(n-1,tab);
    }
Run Code Online (Sandbox Code Playgroud)

c c++ recursion

-2
推荐指数
1
解决办法
129
查看次数

返回语句未按预期返回

为什么下面的代码返回-1而不是arr.length-1?如果find()方法查找24,则应返回5,但现在返回-1.如果在arr中找不到n,它应该只返回-1.

public class linearArraySearch {

    public static void main(String[] args) {
        int[] numbers = new int[]{ 12, 42, 56, 7, 99, 24, 6, 1, 5 };
        System.out.println( find(numbers, 24) );
    }

    public static int find( int[] arr, int n ){
        if( arr[arr.length-1] == n ){
            return arr.length-1;
        }else if( arr.length > 1 && arr[arr.length-1] != n ){
            int[] temp = new int[arr.length-1];
            for( int i = 0; i < temp.length; i++ ){
                temp[i] = arr[i];
            } …
Run Code Online (Sandbox Code Playgroud)

java recursion return linear-search

-2
推荐指数
1
解决办法
80
查看次数

找Palindrome

我想找一个单词的Palindrome.这里有什么不对吗?

主功能:

  int size;
  string input;
  cin>>input;
  size = input.length();  
  if(testPalindrome(input,size-1,0))
    cout<<"It's Palindrome";
  else
    cout<<"It's not Palindrome";
Run Code Online (Sandbox Code Playgroud)

testPalindrome函数是:

bool testPalindrome (string pal , int last, int first){

    if (pal[first] != pal[last])
        return false;
    else{
        if (first<last)
            testPalindrome(pal,last-1,first+1);
        else
            return true;
    }
}
Run Code Online (Sandbox Code Playgroud)

我已阅读此链接并找到了确定Palindromes的答案,但为什么这个不起作用?

c++ string recursion function palindrome

-2
推荐指数
1
解决办法
255
查看次数

循环的迭代

函数f由规则定义

在此输入图像描述

写一个函数f(n),f通过迭代过程计算

我写了这个.并且仍然没有做对.请告知如何解决它.

def f(n):
    if (n<3):
        return n
    else:
        for x in range (n):
            a = f(n-1) + 2*f(n-2) + 3*f(n-3)
            return (a)
Run Code Online (Sandbox Code Playgroud)

python algorithm recursion performance

-2
推荐指数
1
解决办法
76
查看次数

递归代码很难理解 - 需要简化

我有一个实体,我可以拥有InvoiceLine,然后您可以无限次地信用该发票行.但只有主InvoiceLine才能引用原始实体.

我使用递归来获取原始实体,但代码不是那么可读

private static PermanentPlacement PopulatePermanentPlacement(InvoiceLine invoiceLine)
        {
            PermanentPlacement permanentPlacement;
            var creditReissue = invoiceLine.CreditReissue;
            do
            {
                permanentPlacement = creditReissue.InvoiceLine.PermanentPlacement;
                if (permanentPlacement == null)
                {
                    creditReissue = creditReissue.InvoiceLine.CreditReissue;
                }
            } while(permanentPlacement == null);

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

有什么方法可以让我更可读和简化?

c# recursion

-2
推荐指数
1
解决办法
221
查看次数

为什么这个递归有效?

我对此递归感到有点困惑:

def count(num):
    if num == 0:
        print('Go!',end=' ')
    else:
        count(num-1)
        print(num,end=' ')
count(5)
Run Code Online (Sandbox Code Playgroud)

为什么这样做?打印"Go!"后程序不应该停止执行吗?

当我在python可视化工具中运行它时,打印出"Go!" 执行跳转到不应该发生的else语句...

例如,它打印"Go!1 2 3 4 5"但我希望它打印"Go!"

python recursion logic integer function

-2
推荐指数
1
解决办法
64
查看次数

使用if语句进行Python递归函数调用

我有一个关于使用if语句和递归的函数调用的问题.我有点困惑,因为即使我的函数返回"False",python似乎也会跳转到if语句块

这是一个例子:

1    def function_1(#param):
2       if function_2(#param):
3           #do something
4           if x<y:
5               function_1(#different parameters)
6           if x>y:
7               function_1(#different parameters)
Run Code Online (Sandbox Code Playgroud)

我的function_2返回"False",但python继续第5行的代码.谁能解释这种行为?提前感谢您的任何答案.

编辑:对不起,忘了括号

具体例子:

1    def findExit(field, x, y, step):
2        if(isFieldFree(field, x, y)):
3            field[y][x] = filledMarker
4            findExit(field, x + 1, y, step+1)
5            findExit(field, x - 1, y, step+1)
6            findExit(field, x, y + 1, step+1)
7            findExit(field, x, y - 1, step+1)
8        elif(isFieldEscape(field, x, y)):
9            way.append(copy.deepcopy(field))
10            wayStep.append(step+1)

    def isFieldFree(field, x, y): …
Run Code Online (Sandbox Code Playgroud)

python recursion if-statement function

-2
推荐指数
1
解决办法
1494
查看次数

在运行C递归程序时转储核心

当我运行以下C程序时,编译器显示分段错误(核心转储).我不明白为什么会这样,我怎么能检索这段代码.

#include <stdio.h>
int power(int x, int n)
{
  if (n = 0)
    return 1;
  else
    return x * power (x, n - 1);
}

int main(void)
{
  int x=3,n=4;
  printf("the answer is:%d\n",power(3,4));
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

c recursion

-2
推荐指数
1
解决办法
95
查看次数