将响应限制为每行十个五边形数

limiting responses to ten Pentagonal numbers per line

本文关键字:十个 五边形 响应      更新时间:2023-10-16

我正在编写一个代码,要求每行五边形数限制为10,但我无法使其工作。我的代码是:

#include <iostream>
using namespace std;
int getPentagonalNumber(int n)
{
    /*equation to calculate pentagonal numbers*/
    return (n * (3 * n - 1) / 2);
}
int main()
{
    /*Ask the user to put in the number of results*/
    int userInput;
    cout << "How many pentagonal numbers would you like to be displayed: ";
    cin >> userInput;
    cout << endl;
    cout << "results are: " << endl;
    /*Loop to generate the numbers for the equation*/
    for (int n = 1; n <= userInput; n++)
    {
        cout << getPentagonalNumber(n) << " ";
    }
    return 0;
}

这能满足你的要求吗:

#include <iostream>
using namespace std;
int getPentagonalNumber(int n)
{
    /*equation to calculate pentagonal numbers*/
    return (n * (3 * n - 1) / 2);
}
int main()
{
    /*Ask the user to put in the number of results*/
    int userInput;
    cout << "How many pentagonal numbers would you like to be displayed: ";
    cin >> userInput;
    cout << endl;
    cout << "results are: " << endl;
    /*Loop to generate the numbers for the equation*/
    for (int n = 1; n <= userInput; n++)
    {
        cout << getPentagonalNumber(n) << " ";
        if (n % 10 == 0)
           cout << endl;
    }
    return 0;
}

每当n被10整除时,我就添加一个新行输出。