#include <bits/stdc++.h>
using namespace std;
int main() {
int p1, p2, p3;
cin >> p1 >> p2 >> p3;
string s;
cin >> s;
function<int(char)> type = [](char c) {
if (isalpha(c)) return 1;
if (isdigit(c)) return 2;
return 0;
};
function<char(char)> into_by_p1 = [p1](char c) {
if (p1 == 3) return '*';
if (isdigit(c)) return c;
if (p1 == 1) return (char)tolower(c);
return (char)toupper(c);
};
for (int i = 0; i < s.size(); i++) {
bool is_range = s[i] == '-' &&
i != 0 &&
i != s.size() - 1 &&
type(s[i - 1]) != 0 &&
type(s[i - 1]) == type(s[i + 1]) &&
s[i - 1] < s[i + 1];
if (!is_range) {
cout << s[i];
continue;
}
int start = s[i - 1];
int end = s[i + 1];
int step = 1;
if (p3 == 2) swap(start, end), step = -1;
for (char c = start + step; c != end; c += step) {
const char c_out = into_by_p1(c);
for (int i = 0; i < p2; i++) cout << c_out;
}
}
return 0;
}
题目不难,就是个复杂一点的模拟,但是直接写出来代码嵌套会很深,可读性差,还容易出错。使用提前处理和策略模式,降低p1、p2、p3之间的依赖性,减少嵌套。

京公网安备 11010502036488号