1 条题解
-
0
练23.4 首字母
解题思路
分别对字符 a、b、c 输出对应单词,否则输出 no。
正确性说明
四个互斥分支直接实现题目映射。
易错点
只识别小写字母。
复杂度
输入规模固定,时间 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; cout << (c == 'a' ? "apple" : c == 'b' ? "banana" : c == 'c' ? "cat" : "no") << "\n"; return 0; }
- 1