c++ - 判断一个序列(整数)是否单调递增
问题描述
#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”循环,它每次只比较两个数字,而不是将所有数字一起比较。请你帮助我好吗?谢谢::>
解决方案
您最初使用 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.
您也可以将[]
like 与数组一起使用来访问向量的现有元素,但使用.at
可能更可取,因为您可以进行边界检查。
推荐阅读
- python - 检查列表是否包含除“apples”和“oranges”之外的其他字符串
- autodesk-forge - Download a Document with Autodesk API
- ajax - 如何使用 Django 和 AJAX 将数据插入数据库?
- android - Extract the data from QR Codes and create a new QR code with colour
- python - How to write from multiple Tkinter GUI windows to an Excel file at a time?
- php - PHP / HTML在提交后记住选择的值
- machine-learning - 预测句子中的缺失词 - 自然语言处理模型
- javascript - HTML Bootstrap Modal 未出现在变量 javascript 中
- react-native - 可能的未处理承诺拒绝(id:0):错误:权限被拒绝(通过“CameraRoll.saveToCameraRoll()”保存图像时)
- reactjs - 通过 POST 将额外的值传递给另一个函数