题解 | #坐标移动#
坐标移动
https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
#include<iostream>
#include <string>
#include<cstring>
#include<vector>
using namespace std;
int main()
{
char arr[100];//="A10;S20;W10;D30;X;A1A;B10A11;;A10;"; // A10;S20;W10;D30;X;A1A;B10A11;;A10;
cin>>arr;
vector<string> vec;
char s[] = ";";
char *ptr;
int x=0,y=0;
/* 获取第一个子字符串 */
ptr = strtok(arr, s);
/* 继续获取其他的子字符串 */
while( ptr != NULL )
{
//cout<<ptr<<endl;
vec.push_back(ptr);
ptr = strtok(NULL, s);
}
// for(int j=0;j<vec.size();j++)
// cout<<vec[j]<<endl;
for(vector<string>::iterator it=vec.begin();it!=vec.end();it++)
{
string str=*it;
if(str.size()==3 && isupper(str[0]) && isdigit(str[1]) && isdigit(str[2]))
{
switch(str[0])
{
case 'A':
x = x-(10 *int(str[1]-'0')+(str[2]-'0'));
// cout<<"(10 *(str[1]):"<<(10 *(str[1])<<endl;
//cout<<"x:"<<x<<endl;
break;
case 'S':
y = y-(10 *int(str[1]-'0')+(str[2]-'0'));
// cout<<"y:"<<x<<endl;
break;
case 'W':
y = y+(10 *int(str[1]-'0')+(str[2]-'0'));
// cout<<"x:"<<x<<endl;
break;
case 'D':
x = x+(10 *int(str[1]-'0')+(str[2]-'0'));
// cout<<"x:"<<x<<endl;
break;
}
}
if(str.size()==2 && isupper(str[0]) && isdigit(str[1]))
{
switch(str[0])
{
case 'A':
x = x-int(str[1]-'0');
// cout<<"x:"<<x<<endl;
break;
case 'S':
y = y-int(str[1]-'0');
// cout<<"y:"<<x<<endl;
break;
case 'W':
y = y+int(str[1]-'0');
// cout<<"x:"<<x<<endl;
break;
case 'D':
x = x+int(str[1]-'0');
// cout<<"x:"<<x<<endl;
break;
}
}
//cout<<str<<endl;
}
cout<<x<<","<<y<<endl;
//cout<<*it<<endl;
return 0;
}

