The string of numbers ends with the value 0, where 0 is not part of the heights (only marks the end of the string).
Specify whether the range of heights is ascending, descending, or non-monotonous.
Input Output
1 2 5 5 10 11 0 ascending
16 7 3 0 descending
1 2 2 1 0 non-monotonous
Solving as much as possible with while and if.
Restrictions and clarifications:
- 0 is not part of the string of heights, only marks the end of the string
-heights are numbers between 0 and 10,000
#include <iostream>
using std::cin;
using std::cout;
int main()
{
unsigned n, last;
while (cin >> last) { // get first number if this set
unsigned flags = 0;
constexprunsigned Increasing = 1;
constexprunsigned Decreasing = 2;
while (cin >> n) {
if (n == 0) break; // end of input
if (n > last) {
flags |= Increasing; // we saw it going up
} elseif (n < last) {
flags |= Decreasing; // we saw it going down
}
last = n;
}
constchar *classification[4] = {
"even",
"ascending",
"descending",
"non-monotonic"
};
cout << classification[flags] << '\n';
}
}