我需要编写一个程序来计算从 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/