题解 | #坐标移动#

坐标移动

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;
}
全部评论

相关推荐

11-30 11:07
河南大学 Java
宇宙厂 测开 n*15
丘丘给个offer:有后选后
点赞 评论 收藏
分享
程序员猪皮:看不到八股什么意思
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务