gpt4 book ai didi

C:None-Recursive函数变为递归函数

转载 作者:行者123 更新时间:2023-11-30 14:49:26 25 4
gpt4 key购买 nike

我需要编写一个程序来计算从 0 到 n 的数字的平方。我知道如何使用迭代来完成它,但如何使用递归重写注释函数?我觉得这是一个简单的任务,我应该毫无问题地处理它,但是出了问题。

#include <stdio.h>
#include<conio.h>

int main()
{
int x=0, n,m;
printf("Enter last integer ");
scanf_s("%d\n", &n);
while (x < n) /* How to rewrite this function using recursion?*/
{
printf("\n%d\n", x*x);
x++;
}
_getch();
return 0;
}

最佳答案

您可以使用以下功能:

void function(int i, int n)
{
if(i < n)
{
printf("\n%d\n", i*i);
function(i + 1, n);
}
else
return;
}

然后调用function(0, n);

关于C:None-Recursive函数变为递归函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49598170/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com