题解 | #坐标移动#

坐标移动

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

#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main(int argc, char* argv[]){
    string str;
    getline(cin, str);
    vector<string> move;
    for(int i = 0; i < str.size(); ++i){
        string temp;
        while(str[i] != ';'){
            temp.push_back(str[i]);
            ++i;
        }
        move.push_back(temp);
    }
    int x = 0;
    int y = 0;
    for(int i = 0; i < move.size(); ++i){
        if(move[i].size() == 2 || move[i].size() == 3){
            if(move[i][0] == 'A' || move[i][0] == 'D' || move[i][0] == 'W' || move[i][0] == 'S'){
                int length1 = move[i][1] - '0';
                if(move[i].size() == 2 && length1 >= 0 && length1 <= 9){
                    switch(move[i][0]){
                        case 'A':
                            x -= length1;
                            break;
                        case 'D':
                            x += length1;
                            break;
                        case 'W':
                            y += length1;
                            break;
                        case 'S':
                            y -= length1;
                            break;
                        default:
                            break;
                    }
                }
                else{
                    int length2 = move[i][2] - '0';
                    if(length1 >= 0 && length1 <= 9 && length2 >= 0 && length2 <= 9){
                        int length = 10 * length1 + length2;
                        switch(move[i][0]){
                            case 'A':
                                x -= length;
                                break;
                            case 'D':
                                x += length;
                                break;
                            case 'W':
                                y += length;
                                break;
                            case 'S':
                                y -= length;
                                break;
                            default:
                                break;
                    }
                    }
                }
            }
        }
    }
    cout << x << "," << y << endl;
}
全部评论

相关推荐

06-12 17:46
门头沟学院 Java
运营你豪哥:来说重点: ​1.项目前置,时间倒序。​​ 2.​项目描述强化结果与量化效果(STAR原则里的R)。​​ ​3.个人技能精炼,明确掌握程度,突出核心。​​ ​4.增加强有力开头的个人总结部分。​​ 5.​优化教育背景(成绩排名)、合并奖项与活动。​​
听劝,我这个简历该怎么改...
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务