递归函数计算序列中的平方和(并输出过程)

Recursion function to calculate the sum of squares in a sequence (and output the process)

本文关键字:输出 过程 计算 递归函数      更新时间:2023-10-16
int SeriesToOne(int n) {
if (n == 1) {
cout << "(" << 1 << "*" << 1 << ") = ";
return 1;
}
cout << "(" << n << "*" << n << ") + ";
return (n * n) + SeriesToOne(n - 1); }

嘿,我正在写一个程序,它应该使用递归计算序列中的平方和。

我正在编写两个函数,一个计算它从1到N的过程,另一个计算从N到1的过程,并输出过程。上面的代码是我为N到1编写的函数,但我在从1到N的过程中遇到了很多麻烦。

我不知道如何在不向函数中添加第二个参数(赋值指定了一个参数(的情况下正确地写出基本情况并进入该基本情况。

如果有人能帮我,那就太棒了!很抱歉,如果我没有格式化这篇文章或把它放在错误的部分,第一次海报。

这是用javascript实现的。

function SeriesToN(n, initialCount){
if(!initialCount){
initialCount  = 1;
}
if(initialCount === n){
return n * n ;
}
return (initialCount * initialCount) + SeriesToN(n , initialCount + 1)
}
SeriesToN(5)

好的,由于基本条件的原因,您需要知道递归函数中n的值,您可以将变量声明为全局变量,也可以在每次调用中将其传递给函数,如下所示:

int SeriesToN(int n, int N) {
if (n == N) {
cout << "(" << n << "*" << n << ") = ";
return n * n;
}
cout << "(" << n << "*" << n << ") + ";
return (n * n) + SeriesToN(n + 1, N); 
}

如果n=4:,则调用如下函数

SeriesToN(1, 4);

这应该能解决你的问题。编码快乐!

#include <iostream>
#include <string>
using namespace std;
int SeriesToN(int n, int limit = -1) 
{   
// catch edge cases
if (n == 1) {
cout << "(1 * 1) = ";
return 1;
} else if (n <= 0) {
cout << "n should be >= ";
return 1;
}
// the actual action
if (limit == 0) 
{
cout << "(" << n << "*" << n << ") = ";
return n * n;
} else if (limit == -1) // handling one argument input
{
limit = n - 1;
n = 1;
cout << "(" << 1 << "*" << 1 << ") + ";
return (1) + SeriesToN(2, limit - 1);         
} else
{   
cout << "(" << n << "*" << n << ") + ";
return (n * n) + SeriesToN(n + 1, limit - 1); 
}
};

int main()
{
cout << SeriesToN(10);
cout << "n";
cout << SeriesToN(3);
cout << "n";
cout << SeriesToN(2);
cout << "n";
cout << SeriesToN(1);
cout << "n";
cout << SeriesToN(0);
cout << "n";
cout << SeriesToN(-1);
cout << "n";
return 0;
}
/* output:
(1*1) + (2*2) + (3*3) + (4*4) + (5*5) + (6*6) + (7*7) + (8*8) + (9*9) + (10*10) = 385
(1*1) + (2*2) + (3*3) = 14
(1*1) + (2*2) = 5
(1 * 1) = 1
n should be >= 1
n should be >= 1
*/

对于递归函数来说,这不是一个好的实现,但它可以完成以下任务:

int SeriesToN(int n) {
static int N = n;
static bool firstTime = true;
if (n == N && !(firstTime)) {
cout << "(" << n << "*" << n << ") = ";
return (n*n);
}
else {
if (n == N) {
firstTime = false;
n = 1;
}
cout << "(" << n << "*" << n << ") + ";
return (n * n) + SeriesToN(n + 1);
}
}

正如你所希望的,它从1到n计数,只有一个参数,没有任何全局变量。

我支持你!只需要在单个帧中执行尾部和头部递归。就是这样。:D

您可以使用以下代码片段:

int SeriesToOne(int n) {
if (n == 0) return 0;
cout<<endl<<n+" : "+(n * n);

int sum = 0;
sum += n * n;
sum += seriesToOne(n-1); // due to loose overhead when n will become 0.
cout<<endl<<n+" : "+(n * n);
sum += n * n;
return sum; // return the total sum we get after two functionality (tail-head recur.)
}

编辑:我不是c++的朋友,忽略所有语法错误,如果有的话!

您拥有的是

  • 做这项工作(打印n术语(,然后做剩余的工作(从n-11(

而您想要的是

  • 先做初始工作(从1n-1(,然后再做这项工作(打印n术语(

将递归结果保存在变量中(并将=位留给main,后者负责打印结果(:

int SeriesToOne(int n) {
if (n == 1) {
cout << "(" << 1 << "*" << 1 << ")";
return 1;
}
cout << "(" << n << "*" << n << ") + ";
int sum = (n * n) + SeriesToOne(n - 1);
return sum;
}

然后您可以更改工作顺序(并移动+输出(:

int SeriesFromOne(int n) {
if (n == 1) {
cout << "(" << 1 << "*" << 1 << ")";
return 1;
}
int sum = (n * n) + SeriesFromOne(n - 1);
cout << " + (" << n << "*" << n << ")";
return sum;
}