华为机考真题 | 机器人移动题解 | 坐标移动
坐标移动
https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
#include <iostream>
#include<bits/stdc++.h>
#include<string.h>
using namespace std;
bool isNum (string s){
int len = s.size();
for (int i = 0;i<len;i++){
if('0' <= s[i]&& s[i] <= '9')
continue;
else
return false;
}
return true;
}
int main() {
string s;
pair<int,int> p(0,0);
while (getline(cin,s,';')){
if(s.empty())
continue;
if(s.size()<2)
continue;
string s1 = s.substr(1);
if(isNum(s1)){
switch(s[0]){
case 'A': p.first-=stoi(s1);
break;
case 'D': p.first+=stoi(s1);
break;
case 'W': p.second+=stoi(s1);
break;
case 'S': p.second-=stoi(s1);
break;
default:
break;
}
}
}
cout<<p.first<<","<<p.second<<endl;
return 0;
}
// 64 位输出请用 printf("%lld")