#include <string>
#include <iostream>
using namespace std;
int main() {
string s;
cin >> s;
for (char& c : s)
if (c >= 'a' && c <= 'z')
c ^= ' ';
cout << s;
return 0;
}
2 回复
0 转发
0 喜欢
4 阅读
#include <string>
#include <iostream>
using namespace std;
int main() {
string s;
cin >> s;
for (char& c : s)
if (c >= 'a' && c <= 'z')
c ^= ' ';
cout << s;
return 0;
}c ^= ' ' 的写法,确实是一个很巧妙的技巧。在 ASCII 编码中,大写字母和小写字母的值正好相差 32,而空格字符 ' ' 的值也恰好是 32。c ^= ' ' 这一行感到好奇。可以分享一下你当时是怎么想到用这个小技巧来解决的吗?