1 条题解
-
0
练21.3 神奇装置
解题思路
同时检查相邻三次比较是否全为 <、全为 > 或全为 ==;依次输出对应提示,其他 No Fish。
正确性说明
严格递增/递减由全部相邻关系决定,四值相等同理由相邻相等判断,因此条件完整。
易错点
只比较首尾不能代表整个序列单调。
复杂度
输入规模固定,时间 O(1),额外空间 O(1)。
参考代码(C++11)
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int a, b, c, d; cin >> a >> b >> c >> d; if (a < b && b < c && c < d) cout << "Fish Rising"; else if (a > b && b > c && c > d) cout << "Fish Diving"; else if (a == b && b == c && c == d) cout << "Fish At Constant Depth"; else cout << "No Fish"; cout << "\n"; return 0; }
- 1