【问题标题】:Getting all Polite numbers below a certain number获取低于某个数字的所有礼貌数字
【发布时间】:2021-09-25 01:18:35
【问题描述】:

我正在尝试编写一个 C++ 程序来显示低于某个数字的所有礼貌数字。例如,如果有人要输入 6,那么我想打印出 3、5、6。如果要输入数字 9,我想打印出 3,5,6,7,9。

我已经完成了两个 for 循环。我只是不知道如何测试所有连续数字组合,以获得礼貌的数字。

#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;
int main()
{
cout << "Welcome to the polite numbers program." << endl;

int upValue;
int num1, num2;

cout << "What is the upper value? "; //Ask user for upper limit
cin >> upValue; //Stores user input into upValue

while (upValue < 1) //Validate user input of upper limit
{
    cout << "What is the upper value? (must be an integer > 0) ";
    cin >> upValue;
}

for (num1 = 1; num1 <= upValue; num1++) { //Looping from 1 to upper variable


for (num2 = num1 + 1; num2 <= upValue; num2++) { //Looping from 2 to upper

//Something goes here to get all polite numbers below a certain number.
}
}

return 0;
}

【问题讨论】:

  • 将问题分解为更小的子问题。 IE。创建一个函数来测试一个数字是否礼貌bool is_polite(int)。然后检查所有数字 1-upValue 是否有礼貌。

标签: c++ numbers


【解决方案1】:

您可以将其分解为两个较小的问题。

这是一个实现示例。可能有更有效的方法可以做到这一点,但这应该让您了解需要做什么。

// calculate the sum of all integers between "start" and "end" inclusive.
// hint: the sum of integers between 1 and n is "n(n + 1) / 2".
int sum_between(int start, int end)
{
    return (end * (end + 1) - start * (start - 1)) / 2;
}

// determine if a number polite
bool number_is_polite(int num)
{
    // a little shortcut, we know any number less than 3
    // cannot be polite
    if (num <= 2)
        return false;

    // get the starting point, which must be at least 2 away
    // from the target number
    for (int start = 1; start <= num - 2; start++)
    {
        // get the ending point, which must be at least 1 away
        for (int end = start + 1; end <= num - 1; end++)
        {
            // return true if the sum of integers between start/end
            // inclusive is equal to the target number
            if (sum_between(start, end) == num)
                return true;
        }
    }

    // we searched all consecutive integer subsets but nothing
    // seemed to sum to the target number
    return false;
}

现在我们可以完成您的代码:

#include <iostream>
#include <iomanip>
#include <cmath>

// < insert functions from above >

using namespace std;
int main()
{
    cout << "Welcome to the polite numbers program." << endl;

    int upValue;

    cout << "What is the upper value? "; //Ask user for upper limit
    cin >> upValue; //Stores user input into upValue

    while (upValue < 1) //Validate user input of upper limit
    {
        cout << "What is the upper value? (must be an integer > 0) ";
        cin >> upValue;
    }

    for (int i = 2; i <= upValue; i++)
    {
        if (number_is_polite(i))
            cout << i << " ";
    }

    cout << endl;
}

【讨论】:

  • 提示:礼貌测试比这里显示的要快得多,但我会把它留给你找。我实施这种简单的方法只是为了演示如何解决这个问题。
  • 谢谢,dreamlax。在研究了礼貌的数字之后,我实际上试图实施一种更快的方法。但无论如何,对于这个作业,我必须使用迄今为止在课堂上教过的东西。
猜你喜欢
  • 2021-11-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-08
  • 2014-06-09
  • 1970-01-01
  • 2015-11-13
相关资源
最近更新 更多