求和1到N,打印新行中的每个总和(递归)

时间:2018-03-30 04:06:46

标签: c++ recursion

我有一个基本程序,总计1到N.

但是,我需要打印每行总和为'N'的行。

ex:input - > 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;
    }
}

5 个答案:

答案 0 :(得分:3)

见:

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;
    }
}

输出:

enter image description here

答案 1 :(得分:1)

您可以尝试像存储1 + 2这样的总和。在字符串中并在每次调用时保持附加新数字,并在字符串后的每次调用中输出新的总和

答案 2 :(得分:1)

使用内部值prefix_valprefix_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));
}

DEMO OUTPUT

Sum to: 10
1 = 1
1 + 2 = 3
1 + 2 + 3 = 6
1 + 2 + 3 + 4 = 10
1 + 2 + 3 + 4 + 5 = 15
1 + 2 + 3 + 4 + 5 + 6 = 21
1 + 2 + 3 + 4 + 5 + 6 + 7 = 28
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 = 36
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 = 45
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 = 55

答案 3 :(得分:0)

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

#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);
}

但是有了你的限制,你必须使用这样的递归来模拟for循环:

#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);
}

答案 4 :(得分:-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);

}

输出

Sum to:3
1 = 1
1 + 2 = 3
1 + 2 + 3 = 6