题解 | #坐标移动#
坐标移动
https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
根据;将字符串分割,只取ASWD开头并且长度<=3的处理,我对于非法的没有舍弃,全部向A方向移动0个单位,这样不用犹豫要不要加到数组中 #include<bits/stdc++.h> using namespace std; struct P { char a; int b; }; P l[10005]; int main() { int x = 0, y = 0; string s; cin >> s; istringstream iss(s); string buffer; int i = 0; while (getline(iss, buffer, ';')) { if (buffer.length() == 3 && (buffer[0] == 'A' || buffer[0] == 'S' || buffer[0] == 'W' || buffer[0] == 'D') && buffer[1] >= '0' && buffer[1] <= '9' && buffer[2] >= '0' && buffer[2] <= '9') { int n = (buffer[1] - '0') * 10 + (buffer[2] - '0'); l[i].a = buffer[0]; l[i].b = n; } else if (buffer.length() == 2 && (buffer[0] == 'A' || buffer[0] == 'S' || buffer[0] == 'W' || buffer[0] == 'D') && buffer[1] >= '0' && buffer[1] <= '9') { int n = (buffer[1] - '0'); l[i].a = buffer[0]; l[i].b = n; } else { l[i].a = 'A'; l[i].b = 0; } i++; } for (int j = 0; j < i; j++) { if (l[j].a == 'A') { x -= l[j].b; } if (l[j].a == 'D') { x += l[j].b; } if (l[j].a == 'W') { y += l[j].b; } if (l[j].a == 'S') { y -= l[j].b; } } cout << x << ',' << y << endl; }