题解 | #坐标移动#c++
坐标移动
http://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
#include <iostream>
#include <string>
#include <vector>
using namespace std;
bool isValid(string s){
if(s.size() == 0 || s.size() == 1 || s.size() >=4){
return false;
}
if(s[0]!='A' && s[0]!='D' && s[0]!='W' && s[0]!='S'){
return false;
}
if(s[1]<'1' || s[1] > '9'){
return false;
}
if(s.size()==2){
return true;
}
if(s[2]<'0' || s[2] > '9'){
return false;
}
return true;
}
int main(void){
char ch;
string command;
vector<int> coor(2,0);
while((ch=getchar()) !='\n'){
if(ch!=';'){
command.push_back(ch);
continue;
}
if(isValid(command)){
if(command[0]=='A'){
coor[0] -= stoi(command.substr(1));
}
if(command[0]=='D'){
coor[0] += stoi(command.substr(1));
}
if(command[0]=='W'){
coor[1] += stoi(command.substr(1));
}
if(command[0]=='S'){
coor[1] -= stoi(command.substr(1));
}
}
command.clear();
}
cout<<coor[0]<<','<<coor[1]<<endl;
return 0;
}