1 条题解
-
0
【例21.1】 定制毕业服
解题思路
从矮到高按 150、160、170、180 四个上界依次判断,对应 S、M、L、XL,最后 XXL。
正确性说明
前面未满足意味着已超过前一上界,逐级分支自动构成题目五段。
易错点
每个区间上界包含在该尺寸中。
复杂度
输入规模固定,时间 O(1),额外空间 O(1)。
参考代码(C++11)
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int x; cin >> x; if (x <= 150) cout << "S"; else if (x <= 160) cout << "M"; else if (x <= 170) cout << "L"; else if (x <= 180) cout << "XL"; else cout << "XXL"; cout << "\n"; return 0; }
- 1