ex0615.cpp
来自「《C++编程习题与解答》书中所有例题与习题的源代码」· C++ 代码 · 共 25 行
CPP
25 行
// Programming with C++, Second Edition, by John R. Hubbard
// Copyright McGraw-Hill, 2000
// Example 6.15 on page 136
// Testing an array for monotonicity
#include <iostream>
using namespace std;
bool isNondecreasing(int a[], int n);
// returns true iff a[0] <= a[1] <= ... <= a[n-1];
int main()
{ // tests the isNondecreasing() function:
int a[] = { 22, 44, 66, 88, 44, 66, 55 };
cout << "isNondecreasing(a,4) = " << isNondecreasing(a,4) << endl;
cout << "isNondecreasing(a,7) = " << isNondecreasing(a,7) << endl;
}
bool isNondecreasing(int a[], int n)
{ // returns true iff a[0] <= a[1] <= ... <= a[n-1]:
for (int i=1; i<n; i++)
if (a[i]<a[i-1]) return false;
return true;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?