题解 | #坐标移动#
坐标移动
https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
#include <iostream> #include <vector> #include <string> using namespace std; vector<int> toCoordinate(string s) { int n = s.size(); if (n < 2 || n > 3) return {0,0}; char c = s[0]; if (!(c == 'A' || c == 'D' || c == 'W' || c == 'S')) return {0,0}; if (n == 2) { if (s[1] < '0' || s[1] > '9') return {0,0}; } else { if (s[1] < '1' || s[1] > '9' || s[2] < '0' || s[2] > '9') return {0,0}; } int num=0; if(n==2){ num = s[1]-'0'; } else{ num = (s[1]-'0')*10+(s[2]-'0'); } if(c=='A') return {-num, 0}; else if(c=='D') return {num, 0}; else if(c=='W') return {0, num}; else return {0,-num}; } void splitByLabel(string s, char split, vector<string>& string_vecs) { int n = s.size(); for (int i = 0; i < n;) { int j = i; //while (j < n && s[j] != ';') j++; while(s[j]!=split) j++; string_vecs.push_back(s.substr(i, j - i)); i = j + 1; } } int main() { string s; getline(cin, s); vector<string> vecs; splitByLabel(s,';',vecs); int x=0,y=0; for(int i=0;i<vecs.size();i++){ vector<int>ans = toCoordinate(vecs[i]); //cout<<'('<<ans[0]<<','<<ans[1]<<endl; x+=ans[0]; y+=ans[1]; } cout<<x<<','<<y<<endl; return 0; } // 64 位输出请用 printf("%lld")