坐标移动
程序员文章站
2022-07-13 14:27:13
...
#include <iostream>
#include <string>
#include <cstddef>
using namespace std;
int main () {
string str;
while(cin >> str) {
pair<int , int > point(0, 0);
int found = str.find_first_of(';');
int start = 0;
while(found != str.npos) {
string s1 = str.substr(start, found - start);
start = found + 1;
found = str.find_first_of(';', found + 1);
if (s1.size() >= 2 && s1.size() <= 3)
{
char c = s1[0];
int invalid = 0;
int sum = 0;
for (int i = 1; i < s1.size(); i ++) {
if (s1[i] >= '0' && s1[i] <= '9'){
sum = sum * 10 + (s1[i] - '0');
}
else {
invalid = 1;
break;
}
}
if (invalid == 0) {
switch (c) {
case 'A' :
{
point.first -= sum;
break;
}
case 'D' :
{
point.first += sum;
break;
}
case 'W' :
{
point.second += sum;
break;
}
case 'S' :
{
point.second -= sum;
}
}
}
}
}
cout << point.first << ',' << point.second << endl;
}
return 0;
}