#include <iostream>
#include <vector>
using namespace std;
class Sulotion {
public:
vector<pair<string, int>> ping = {{"abc", 2}, {"def", 3}, {"ghi", 4}, {"jkl", 5},
{"mno", 6}, {"pqrs", 7}, {"tuv", 8}, {"wxyz", 9}
};
string deCode(string& code) {
int size = code.size();
string res = "";
for (int i = 0; i < size; i++) {
if (code[i] >= '0' && code[i] <= '9') {
res += code[i];
} else if (code[i] >= 'a' && code[i] <= 'z') {
for (int j = 2; j < 10; j++) {
if (ping[j - 2].first.find(code[i]) != string::npos) {
res += to_string(j);
}
}
} else if (code[i] >= 'A' && code[i] < 'Z') {
char c = tolower(code[i]);
res += (c - 'a' + 1) % 26 + 'a';
} else if (code[i] == 'Z') {
res += 'a';
}
}
return res;
}
};
int main() {
string line;
getline(cin, line);
Sulotion A;
cout << A.deCode(line) << endl;
return 0;
}
第二个
#include <bits/stdc++.h>
using namespace std;
class My_vector {
public:
My_vector() {};
My_vector(char s, int num) {
switch (s) {
case 'A':
x = -1;
break;
case 'D':
x = 1;
break;
case 'W':
y = 1;
break;
case 'S':
y = -1;
break;
default:
x = 0, y = 0;
break;
}
if (0 < num < 100) {
x *= num;
y *= num;
} else {
x = 0;
y = 0;
}
}
My_vector operator+(const My_vector&
b) { //类内重载,运算符重载函数作为类的成员函数
My_vector ret;
ret.x = this->x + b.x;
ret.y = this->y + b.y;
return ret;
}
int x = 0, y = 0;
};
void split(string str, vector<string>& res) {
char* str_ch = str.data();
char* p = strtok(str_ch, ";");
while (p != NULL) {
string tem(p);
// cout<<tem<<" ";
res.push_back(tem);
p = strtok(NULL, ";");
}
}
int main() {
string str;
vector<string> res;
My_vector ans;
stringstream ss;
while (getline(cin, str)) {
split(str, res);
for (int i = 0; i < res.size(); i++) {
string tem = res[i];
int tem_int;
if (tem.size() == 1 || tem.size() > 3 || tem[1] > 61 || tem[2] > 61)
continue;
tem_int = std::stoi(tem.substr(1));
//cout <<tem_int<<" ";
My_vector temp(tem[0], tem_int);
ans = ans + temp;
}
}
cout << ans.x << "," << ans.y;
}