方法一:使用字符串
注意 :
- 字符与字符相加返回的是一个整数
- 字符串与字符相加的问题:
- 可以使用
+
号运算符,但其中一个操作数需要是string
类型 - 如果需要连续相加多个字符,前两个操作数必须有一个是
string
类型的 - 可以使用
string.append
方法
- 可以使用
- 控制台输入字符串的问题,如果直接使用
cin >> str
,遇到空格和回车都会终止读取
例如:在字符串 s 后面添加 ‘a’、‘b’、‘c’ 三个字符
string s;
s = s + 'a' + 'b' + 'c'; // 正确
s += 'a' + 'b' + 'c'; // 错误
s = 'a' + 'b' + 'c' + s; // 错误
int main(){
string LR = "1625", FB = "3645", AC = "1324";
string mov;
getline(cin, mov);
for (char c : mov) {
string tmp = "";
switch (c)
{
case 'L':
case 'R':
{
if (c == 'L') {
// LR = tmp + LR[3] + LR[0] + LR[1] + LR[2];
LR = LR[3] + string(LR, 0, 3);
}
else
{
// LR = tmp + LR[1] + LR[2] + LR[3] + LR[0];
LR = string(LR, 1, 3) + LR[0];
}
// FB = tmp + FB[0] + LR[1] + FB[2] + LR[3];
FB[1] = LR[1];
FB[3] = LR[3];
// AC = tmp + LR[0] + AC[1] + LR[2] + AC[3];
AC[0] = LR[0];
AC[2] = LR[2];
break;
}
case 'F':
case 'B':
{
if (c == 'F')
{
// FB = tmp + FB[3] + FB[0] + FB[1] + FB[2];
FB = FB[3] + string(FB, 0, 3);
}
else
{
// FB = tmp + FB[1] + FB[2] + FB[3] + FB[0];
FB = string(FB, 1, 3) + FB[0];
}
// LR = tmp + LR[0] + FB[1] + LR[2] + FB[3];
LR[1] = FB[1];
LR[3] = FB[3];
// AC = tmp + AC[0] + FB[0] + AC[2] + FB[2];
AC[1] = FB[0];
AC[3] = FB[2];
break;
}
case 'A':
case 'C':
{
if (c == 'A')
{
AC = tmp + AC[3] + AC[0] + AC[1] + AC[2];
}
else
{
AC = tmp + AC[1] + AC[2] + AC[3] + AC[0];
}
LR = tmp + AC[0] + LR[1] + AC[2] + LR[3];
FB = tmp + AC[1] + FB[1] + AC[3] + FB[3];
break;
}
default:
break;
}
}
cout << LR[0] << LR[2] << FB[0] << FB[2] << LR[3] << LR[1];
return 0;
}
方法二:使用 deque 容器
int main()
{
//string LR = "1625", FB = "3645", AC = "1324";
deque<char> LR, FB, AC;
LR.push_back('1');
LR.push_back('6');
LR.push_back('2');
LR.push_back('5');
FB.push_back('3');
FB.push_back('6');
FB.push_back('4');
FB.push_back('5');
AC.push_back('1');
AC.push_back('3');
AC.push_back('2');
AC.push_back('4');
string mov;
getline(cin, mov);
for (char c : mov) {
//string tmp = "";
char tmp;
switch (c)
{
case 'L':
case 'R':
{
if (c == 'L') {
tmp = LR.back();
LR.pop_back();
LR.push_front(tmp);
//LR = tmp + LR[3] + LR[0] + LR[1] + LR[2];
}
else
{
tmp = LR.front();
LR.pop_front();
LR.push_back(tmp);
//LR = tmp + LR[1] + LR[2] + LR[3] + LR[0];
}
//FB = tmp + FB[0] + LR[1] + FB[2] + LR[3];
FB[1] = LR[1];
FB[3] = LR[3];
//AC = tmp + LR[0] + AC[1] + LR[2] + AC[3];
AC[0] = LR[0];
AC[2] = LR[2];
break;
}
case 'F':
case 'B':
{
if (c == 'F')
{
tmp = FB.back();
FB.pop_back();
FB.push_front(tmp);
//FB = tmp + FB[3] + FB[0] + FB[1] + FB[2];
}
else
{
tmp = FB.front();
FB.pop_front();
FB.push_back(tmp);
//FB = tmp + FB[1] + FB[2] + FB[3] + FB[0];
}
//LR = tmp + LR[0] + FB[1] + LR[2] + FB[3];
LR[1] = FB[1];
LR[3] = FB[3];
//AC = tmp + AC[0] + FB[0] + AC[2] + FB[2];
AC[1] = FB[0];
AC[3] = FB[2];
break;
}
case 'A':
case 'C':
{
if (c == 'A')
{
tmp = AC.back();
AC.pop_back();
AC.push_front(tmp);
//AC = tmp + AC[3] + AC[0] + AC[1] + AC[2];
}
else
{
tmp = AC.front();
AC.pop_front();
AC.push_back(tmp);
//AC = tmp + AC[1] + AC[2] + AC[3] + AC[0];
}
//LR = tmp + AC[0] + LR[1] + AC[2] + LR[3];
LR[0] = AC[0];
LR[2] = AC[2];
//FB = tmp + AC[1] + FB[1] + AC[3] + FB[3];
FB[0] = AC[1];
FB[2] = AC[3];
break;
}
default:
break;
}
}
cout << LR[0] << LR[2] << FB[0] << FB[2] << LR[3] << LR[1];
return 0;
}