题解 | #坐标移动#
坐标移动
https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main(int argc, char* argv[]){
string str;
getline(cin, str);
vector<string> move;
for(int i = 0; i < str.size(); ++i){
string temp;
while(str[i] != ';'){
temp.push_back(str[i]);
++i;
}
move.push_back(temp);
}
int x = 0;
int y = 0;
for(int i = 0; i < move.size(); ++i){
if(move[i].size() == 2 || move[i].size() == 3){
if(move[i][0] == 'A' || move[i][0] == 'D' || move[i][0] == 'W' || move[i][0] == 'S'){
int length1 = move[i][1] - '0';
if(move[i].size() == 2 && length1 >= 0 && length1 <= 9){
switch(move[i][0]){
case 'A':
x -= length1;
break;
case 'D':
x += length1;
break;
case 'W':
y += length1;
break;
case 'S':
y -= length1;
break;
default:
break;
}
}
else{
int length2 = move[i][2] - '0';
if(length1 >= 0 && length1 <= 9 && length2 >= 0 && length2 <= 9){
int length = 10 * length1 + length2;
switch(move[i][0]){
case 'A':
x -= length;
break;
case 'D':
x += length;
break;
case 'W':
y += length;
break;
case 'S':
y -= length;
break;
default:
break;
}
}
}
}
}
}
cout << x << "," << y << endl;
}