题解 | #坐标移动#

坐标移动

http://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29

#include<bits/stdc++.h>
using namespace std;
int main() {
    string str1;
    getline(cin, str1);
    int flag = 0;
    vector<string> zuobiao;
    vector<int> res(2, 0);
    for (int i = 0; i < str1.size(); i++) {
        if (str1[i] == ';') {
            string tmp = str1.substr(flag, i - flag);
            if (tmp.size() == 2 || tmp.size() == 3) {
                if (tmp[0] == 'A' || tmp[0] == 'W' || tmp[0] == 'S' || tmp[0] == 'D') {
                    if (tmp[1] >= '0' && tmp[1] <= '9') {
                        if (tmp.size() == 3) {
                            if (tmp[2] >= '0' && tmp[2] <= '9') {
                                zuobiao.push_back(tmp);
                            }
                        }else{
                            zuobiao.push_back(tmp);
                        }
                    }
                }
            }
            flag = i + 1;
        }
    }
    for (int i = 0; i < zuobiao.size(); i++) {
        char fangxiang = (char)zuobiao[i].at(0);
        int num = atoi(&(zuobiao[i].substr(1, zuobiao[i].size() - 1))[0]);
        switch (fangxiang) {
        case'A':
            res[0] -= num;
            break;
        case'D':
            res[0] += num;
            break;
        case'W':
            res[1] += num;
            break;
        case'S':
            res[1] -= num;
            break;
        default:
            break;
        }
    }
    cout << res[0] << "," << res[1];

    return 0;
}
全部评论

相关推荐

11-18 09:44
Java
小白也想要offer:简历别放洋屁,搞不还还放错了,当然你投外企除外,以上纯属个人观点
点赞 评论 收藏
分享
1 收藏 评论
分享
牛客网
牛客企业服务