题解 | #坐标移动#
坐标移动
http://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
笨人奔办法也是通过了 js
#include <string>
using namespace std;
void step(string s, int *x, int *y)
{
int step;
if (s[0] != 'A' && s[0] != 'S' && s[0] != 'W' && s[0] != 'D') //这里千万注意别用成了||
return;
if (s[1] < '0' || s[1] > '9')
return;
if (s.length() == 2)
step = s[1] - '0';
else if (s.length() == 3) {
if (s[2] < '0' || s[2] > '9')
return;
step = (s[1] - '0') * 10 + (s[2] - '0');
}
switch(s[0]) {
case 'A':
*x -= step;
break;
case 'S':
*y -= step;
break;
case 'W':
*y += step;
break;
case 'D':
*x += step;
break;
}
}
int main()
{
int x = 0, y = 0, i = 0;
string str, temp = "";
cin >> str;
for(; i < str.length(); i++) {
if (str[i] != ';') {
temp += str[i];
} else {
if (temp.length() > 1 && temp.length() <= 3) {
step(temp, &x, &y);
}
temp.clear();
}
}
cout << x << "," << y << endl;
}