这道题其实没什么好说的,唯一一点就是遇到无效的比如字母 X,或者前面的都对但是后来没有分号比如 A111D0,这两种都是无效的,一旦发现这种直接跳过找到下一个分号位置就可以了,接着往下计算。
下面直接给出代码
#include<iostream> #include<string> using namespace std; void GetValue(char N, int num, int &x, int &y) { switch (N) { case 'A': x -= num; break; case 'D': x += num; break; case 'W': y += num; break; case 'S': y -= num; break; default: break; } } int main() { string inStr = ""; string tmp = ""; getline(cin ,inStr); int i, j, num = 0; int x = 0, y = 0; char n = ' '; for (i = 0; i<inStr.size(); i++) { if (inStr[i] == 'A' || inStr[i] == 'S' || inStr[i] == 'W' || inStr[i] == 'D') { n = inStr[i]; for (j = i + 1; j<inStr.size(); j++) { if (inStr[j] >= '0'&&inStr[j] <= '9') { num = num * 10 + inStr[j] - '0'; } else { if (inStr[j] == ';') { GetValue(n, num, x, y); } else { while (inStr[j] != ';') { j++; } } num = 0; break; } } i = j; } else { while (inStr[i] != ';') { i++; } } } cout << x << "," << y << endl; return 0; }