1 条题解
-
0
【例40.1】 字符类型判断
解题思路
将字符分别与大写、小写、数字范围比较,三类均不匹配则为other。三个范围互斥。
正确性说明
计算规则直接来自题目定义。按所述顺序处理时,已完成部分的结果被准确保存在累加量、计数或当前状态中;下一步只加入新增贡献。最终得到完整要求的结果。
易错点
用字符常量进行范围比较;按指定英文输出。
复杂度
时间O(1),空间O(1)。
参考代码(C++11)
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); char c; cin >> c; if (c >= 'A' && c <= 'Z') cout << "upper"; else if (c >= 'a' && c <= 'z') cout << "lower"; else if (c >= '0' && c <= '9') cout << "digit"; else cout << "other"; cout << "\n"; return 0; }
- 1