题解 | #简单密码#
简单密码
https://www.nowcoder.com/practice/7960b5038a2142a18e27e4c733855dac
#include <iostream>
#include <cctype>
#include <string>
using std::cin;
using std::cout;
using std::endl;
using std::string;
// Function to process the string and apply transformations
void process_string(const string& str) {
for (auto it = str.begin(); it != str.end(); it++) {
if (isdigit(*it)) {
cout << *it; // Output digits as they are
} else if (islower(*it)) {
// Convert lowercase letters to numbers according to the pattern
if ('a' <= *it && *it <= 'c') {
cout << 2;
} else if ('d' <= *it && *it <= 'f') {
cout << 3;
} else if ('g' <= *it && *it <= 'i') {
cout << 4;
} else if ('j' <= *it && *it <= 'l') {
cout << 5;
} else if ('m' <= *it && *it <= 'o') {
cout << 6;
} else if ('p' <= *it && *it <= 's') {
cout << 7;
} else if ('t' <= *it && *it <= 'v') {
cout << 8;
} else if ('w' <= *it && *it <= 'z') {
cout << 9;
}
} else if (isupper(*it)) {
// Shift uppercase letters by one position, wrap around from 'Z' to 'A'
if (*it == 'Z') {
cout << 'a';
} else {
char ch = (*it) + 1;
ch = tolower(ch);
cout << ch;
}
} else {
cout << *it; // Output other characters as they are
}
}
}
int main() {
string str;
getline(cin, str);
process_string(str);
return 0;
}
查看6道真题和解析