C++ 求和1到N,一路上打印每一个求和到一个新行(递归)

C++ 求和1到N,一路上打印每一个求和到一个新行(递归),c++,recursion,C++,Recursion,我有一个从1到N的基本程序 但是,我需要打印“N”之前的每一行总和 例如:输入->3 1=1 1+2=3 1+2+3=6 我需要通过递归来实现这一点,而不需要在代码中使用任何循环 谢谢你的帮助。我似乎不知道该怎么做 #include <iostream> using namespace std; int sumToN(int n, int row); int input; int main() { cout << "Sum to: "; cin

我有一个从1到N的基本程序

但是,我需要打印“N”之前的每一行总和

例如:输入->3

1=1

1+2=3

1+2+3=6

我需要通过递归来实现这一点,而不需要在代码中使用任何循环

谢谢你的帮助。我似乎不知道该怎么做

#include <iostream>

using namespace std;

int sumToN(int n, int row);

int input;

int main() 
{
    cout << "Sum to: ";
    cin >> input;
    cout << sumToN(input, 1);
}

int sumToN(int n, int row)
{

    if (row==n) // BASE CASE
    {
        cout << row << " = ";
        return row;
    }


    else // RECURSIVE CASE
    {
        cout << row << " + ";
        return (row + sumToN(n, row+1));
        cout << endl;
    }
}

您可以尝试将总和存储为1+2。。在一个字符串中,在每次通话中不断追加新号码,并在每次通话中在该字符串后输出新的总和。您可以尝试类似于将总和存储为1+2的方法。。在一个字符串中,在每次调用中不断追加新号码,并在字符串后输出每次调用中的新和。通过循环,您可以执行以下操作:

#include <iostream>

using namespace std;

void displayToN(const int last) {
    int sum = 0;
    for (int i=1; i<last; sum += i, i++)
        cout << i << " + ";
    cout << last << " = " << (sum + last) << endl;
}

void sumToN(const int curr, const int last) {
    for (int i=curr; i<=last; i++)
        displayToN(i);
}

int main() {
    int input;
    cout << "Sum to: ";
    cin >> input;
    sumToN(1, input);
}
#include <iostream>

using namespace std;

void displayToN(const int curr, const int last, const int sum = 0) {
    if (curr < last) {
        cout << curr << " + ";
        displayToN(curr + 1, last, sum + curr);
    } else {
        cout << curr << " = " << (sum + curr) << endl;
    }
}

void sumToN(const int curr, const int last) {
    if (curr <= last) {
        displayToN(1, curr);
        sumToN(curr + 1, last);
    }
}

int main() {
    int input;
    cout << "Sum to: ";
    cin >> input;
    sumToN(1, input);
}
但是,由于您的局限性,您必须模拟具有如下递归的循环:

#include <iostream>

using namespace std;

void displayToN(const int last) {
    int sum = 0;
    for (int i=1; i<last; sum += i, i++)
        cout << i << " + ";
    cout << last << " = " << (sum + last) << endl;
}

void sumToN(const int curr, const int last) {
    for (int i=curr; i<=last; i++)
        displayToN(i);
}

int main() {
    int input;
    cout << "Sum to: ";
    cin >> input;
    sumToN(1, input);
}
#include <iostream>

using namespace std;

void displayToN(const int curr, const int last, const int sum = 0) {
    if (curr < last) {
        cout << curr << " + ";
        displayToN(curr + 1, last, sum + curr);
    } else {
        cout << curr << " = " << (sum + curr) << endl;
    }
}

void sumToN(const int curr, const int last) {
    if (curr <= last) {
        displayToN(1, curr);
        sumToN(curr + 1, last);
    }
}

int main() {
    int input;
    cout << "Sum to: ";
    cin >> input;
    sumToN(1, input);
}

使用循环,您可以执行以下操作:

#include <iostream>

using namespace std;

void displayToN(const int last) {
    int sum = 0;
    for (int i=1; i<last; sum += i, i++)
        cout << i << " + ";
    cout << last << " = " << (sum + last) << endl;
}

void sumToN(const int curr, const int last) {
    for (int i=curr; i<=last; i++)
        displayToN(i);
}

int main() {
    int input;
    cout << "Sum to: ";
    cin >> input;
    sumToN(1, input);
}
#include <iostream>

using namespace std;

void displayToN(const int curr, const int last, const int sum = 0) {
    if (curr < last) {
        cout << curr << " + ";
        displayToN(curr + 1, last, sum + curr);
    } else {
        cout << curr << " = " << (sum + curr) << endl;
    }
}

void sumToN(const int curr, const int last) {
    if (curr <= last) {
        displayToN(1, curr);
        sumToN(curr + 1, last);
    }
}

int main() {
    int input;
    cout << "Sum to: ";
    cin >> input;
    sumToN(1, input);
}
但是,由于您的局限性,您必须模拟具有如下递归的循环:

#include <iostream>

using namespace std;

void displayToN(const int last) {
    int sum = 0;
    for (int i=1; i<last; sum += i, i++)
        cout << i << " + ";
    cout << last << " = " << (sum + last) << endl;
}

void sumToN(const int curr, const int last) {
    for (int i=curr; i<=last; i++)
        displayToN(i);
}

int main() {
    int input;
    cout << "Sum to: ";
    cin >> input;
    sumToN(1, input);
}
#include <iostream>

using namespace std;

void displayToN(const int curr, const int last, const int sum = 0) {
    if (curr < last) {
        cout << curr << " + ";
        displayToN(curr + 1, last, sum + curr);
    } else {
        cout << curr << " = " << (sum + curr) << endl;
    }
}

void sumToN(const int curr, const int last) {
    if (curr <= last) {
        displayToN(1, curr);
        sumToN(curr + 1, last);
    }
}

int main() {
    int input;
    cout << "Sum to: ";
    cin >> input;
    sumToN(1, input);
}
见此:

int sumToN(int n, int row);

void out(int n);

int input;

int main() 
{
    cout << "Sum to: \n";
    cin >> input;
    out(1);

}

void out(int n)
{
    if( n > input) return;
    cout << sumToN(n, 1)<<"\n";
    out(n+1);
}
int sumToN(int n, int row)
{

    if (row==n) // BASE CASE
    {
        cout << row << " = ";
        return row;
    }


    else // RECURSIVE CASE
    {
        cout << row << " + ";        
         return (row + sumToN(n, row+1));
        cout << endl;
    }
}
输出:

见此:

int sumToN(int n, int row);

void out(int n);

int input;

int main() 
{
    cout << "Sum to: \n";
    cin >> input;
    out(1);

}

void out(int n)
{
    if( n > input) return;
    cout << sumToN(n, 1)<<"\n";
    out(n+1);
}
int sumToN(int n, int row)
{

    if (row==n) // BASE CASE
    {
        cout << row << " = ";
        return row;
    }


    else // RECURSIVE CASE
    {
        cout << row << " + ";        
         return (row + sumToN(n, row+1));
        cout << endl;
    }
}
输出:

使用内部值前缀val和前缀str存储前缀递归结果。然后我们可以输出整个递归工作流

int sumToN(int n, int row, int prefix_val, const std::string& prefix_str);

int main()
{
    // Meanwhile you should void using global variables.
    int input;    
    cout << "Sum to: ";
    cin >> input;
    sumToN(input, 1, 0, "");
}

int sumToN(int n, int row, int prefix_val, const std::string& prefix_str)
{
    string gap;
    if (prefix_val == 0) {
        gap = "";
    } else {
        gap = " + ";
    }

    cout << prefix_str << gap << row << " = " << prefix_val + row << std::endl;
    if (row == n) // recursive end
    {
        return 0;
    }

    return sumToN(n, row + 1, row + prefix_val, prefix_str + gap + std::to_string(row));
}
使用内部值前缀val和前缀str存储前缀递归结果。然后我们可以输出整个递归工作流

int sumToN(int n, int row, int prefix_val, const std::string& prefix_str);

int main()
{
    // Meanwhile you should void using global variables.
    int input;    
    cout << "Sum to: ";
    cin >> input;
    sumToN(input, 1, 0, "");
}

int sumToN(int n, int row, int prefix_val, const std::string& prefix_str)
{
    string gap;
    if (prefix_val == 0) {
        gap = "";
    } else {
        gap = " + ";
    }

    cout << prefix_str << gap << row << " = " << prefix_val + row << std::endl;
    if (row == n) // recursive end
    {
        return 0;
    }

    return sumToN(n, row + 1, row + prefix_val, prefix_str + gap + std::to_string(row));
}
这很有效

#include<iostream>

using namespace std;

int input;


int sumToN(int n, int row)
{

    if (row==n) // BASE CASE
    {
        cout << row << " = ";
        //cout<< row<<endl;
        return row;
    }


    else // RECURSIVE CASE
    {
        cout << row << " + ";
        return (row + sumToN(n, row+1));
        cout << endl;
    }
}

void caller(int n,int current){
    if(current==n){
        cout<<sumToN(n,1)<<endl;
    }
    else{

        cout<<sumToN(current,1)<<endl;
        caller(n,current+1);
    }

}

int main() 
{
    cout << "Sum to: "<<endl;
    cin >> input;
    caller(input, 1);

}
这很有效

#include<iostream>

using namespace std;

int input;


int sumToN(int n, int row)
{

    if (row==n) // BASE CASE
    {
        cout << row << " = ";
        //cout<< row<<endl;
        return row;
    }


    else // RECURSIVE CASE
    {
        cout << row << " + ";
        return (row + sumToN(n, row+1));
        cout << endl;
    }
}

void caller(int n,int current){
    if(current==n){
        cout<<sumToN(n,1)<<endl;
    }
    else{

        cout<<sumToN(current,1)<<endl;
        caller(n,current+1);
    }

}

int main() 
{
    cout << "Sum to: "<<endl;
    cin >> input;
    caller(input, 1);

}

保存n当前结果和当前iterrations保存n当前结果和当前iterrationEdited:for no loop:询问Sum up to的原因是什么:在主例程中,如果您从未使用过,则执行out1;相反?@luiscolrado:“Out”是一个不允许的递归调用循环-OP,它也调用“sum2N”。是的,但您将其称为Out1,而不是应该调用的Outinput。这样,当我遵循程序时,您将始终打印从1到1的和,不再打印。我认为这是您在编辑代码或从测试中快速剪切粘贴代码时犯的错误。。。。我知道它是递归的,但您需要在某个地方使用输入值,并且在代码中的任何地方都没有对它的引用。OP使用“input”作为全局变量,调用Out1,n最初将被指定为1,它将递增,直到超过“input”。编辑:没有循环:问总结的原因是什么:在主程序中,如果你从来没有使用过,然后做一个总结1;相反?@luiscolrado:“Out”是一个不允许的递归调用循环-OP,它也调用“sum2N”。是的,但您将其称为Out1,而不是应该调用的Outinput。这样,当我遵循程序时,您将始终打印从1到1的和,不再打印。我认为这是您在编辑代码或从测试中快速剪切粘贴代码时犯的错误。。。。我知道它是递归的,但您需要在某个地方使用输入值,并且在代码中的任何地方都没有对它的引用。OP使用“input”作为全局变量,调用Out1,n最初将被指定为1,它将递增,直到超过“input”。它可以工作。@LuisColorado删除了循环。@Keval,删除了注释:@LuisColorado删除了循环。@Keval,删除了注释: