两个简化代码的方式:

  1. 可以检测除了首字母以外是否有小写字母,如果没有,则选择翻转;
  2. 对字符异或 32 就可以转换大小写,无需分类讨论
#include <cctype>
#include <iostream>
using namespace std;

string s;

void Solve() {
    cin >> s;
    bool flip = true;
    for (int i = 1; i < s.length(); i++) {
        if (islower(s[i])) {
            flip = false;
            break;
        }
    }
    if (flip) {
        for (auto& c : s) {
            c ^= 'A' ^ 'a';
        }
    }
    cout << s;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    Solve();
    return 0;
}