确定序列(整数)是否单调递增

时间:2020-03-10 15:50:42

标签: c++ arrays

 #include <iostream>

using namespace std;

int main()
{
    int n;
    bool error;
    do
    {
        cout << "How many numbers would you like to put in this sequence? "<< endl;
        cin >> n;
        error = cin.fail() || cin.peek() != '\n' || (n < 0);
        if (error)
        {
            cout << "Please input again" << endl;
        }
        cin.clear();
        cin.ignore(999,'\n');
    }while(error);

    int a[n];
    cout << "Please input the numbers in your sequence." << endl;
    for(int i = 0; i < n; i++)
    {
        do
        {
            cin >> a[i];
            error = cin.fail() || cin.peek() != '\n';
            if (error)
            {
                cout << "Please input again" << endl;
            }
            cin.clear();
            cin.ignore(999,'\n');
        }while(error);
    }

    for(int i=0; i<n; i++)
    {
        if (a[i]<a[i+1])
        {
            cout << "The sequence is monotonically increasing." << endl;

        }
        else
        {
            cout << "The sequence is not monotonically increasing." << endl;
        }
    }
}

大家好,我想检查一下此序列(整数)是否单调递增。 而且我不知道应该使用哪种方式,所以我只是选择使用“ For”循环,该循环每次只比较两个数字,而不是将所有数字进行比较。 请你帮助我好吗? 谢谢::>

1 个答案:

答案 0 :(得分:0)

您最初使用for循环的尝试是正确的。修复多次打印的消息的关键是将打印语句(cout << ...)移出循环 。现在,您可能会问:“我该怎么做?”解决此问题的正确方法是否定条件:您如何知道序列不是单调递增? (根据您的代码,我假设您正在寻找一个严格增加的序列。)

您知道,如果序列中的当前项大于或等于序列中的下一项,则该序列不会单调递增。一旦找到一对这样的术语,您将立即知道该序列不满足该属性,并且可以提早退出循环。因此,我们解决该问题的策略是先假定条件为真,再检查条件是否对任何两个元素都不为真,然后再次检查条件并打印结果。

因此,您的最终for循环应如下所示:

bool is_increasing = true; // Begin by assuming the condition is true.
for (int i = 0; i < n - 1; ++i) { // Note the condition. More info below.
  if (a[i] >= a[i + 1]) { // Here we check if the condition is NOT met.
    is_increasing = false;
    break; // This exits the loop. We don't have to keep going because
           // of the way monotonically increasing is defined.
  }
}
// Now we check the condition and present the result.
if (is_increasing) {
  cout << "The sequence is monotonically increasing." << endl;
}
else {
  cout << "The sequence is not monotonically increasing." << endl;
}

您的代码还有另一个问题。您要非常小心不要尝试读取数组末尾的内容。如果我们跟踪您编写的for循环,那么i = n - 1会发生什么?显然,n - 1 < n,因此for循环的主体运行。然后,我们遇到if语句:if a[i] < a[i + 1]。好的,第一部分a[i]很好,因为这确实要求合法的a[n - 1],但是a[i + 1]呢?糟糕,如果n应该是数组的大小,那么a[n]不存在! (请记住,数组是从零开始的!)编程中普遍存在“一对一”错误,但是如果您花时间在代码中进行跟踪,则可以捕获大多数错误。 (请注意,上面我是如何通过更改循环条件来解决此问题的。)

编辑: 正如paddy在评论中指出的那样,您不想在这里使用数组,因为那是非标准的c ++。相反,您想使用std::vector类(如果您是vector,则只使用using namespace std)类。向量与数组非常相似,但是它们可以在运行时根据需要增长和收缩。使用它们的语法也有所不同。以下是如何使用vector的示例:

std::vector<int> a {}; // This constructs an empty vector of ints.
a.push_back(1); // This puts 1 at the end of the vector. In this case,
                // you can access this number at location 0.
a.push_back(2); // This puts 2 at the end.
cout << print(a.at(0)) << endl; // This will print 1.
cout << print(a.at(1)) << endl; // This will print 2.

您也可以像数组一样使用[]来访问向量的现有元素,但是最好使用.at,因为这样可以进行边界检查。