题解 | #坐标移动#
坐标移动
https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
利用getline()函数进行分隔,得到每一个独立的操作码,然后再对操作码进行判断
/*
HW字符串操作1
*/
#include <bits/stdc++.h>
using namespace std;
int main() {
string s,t;
int x=0,y=0;
while(getline(cin,s)){
stringstream ss(s);
while(getline(ss,t,';')){
if(t.empty()||t.length()>3)continue;
string _=t.substr(1);
if(regex_match(_, regex("[0-9]*"))){
switch(t[0]){
case 'A':
x -=stoi(_);
break;
case 'D':
x +=stoi(_);
break;
case 'W':
y +=stoi(_);
break;
case 'S':
y -=stoi(_);
break;
default:
break;
}
}
}
}
cout<<x<<","<<y;
return 0;
}