题解 | #坐标移动#

坐标移动

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

#include <iostream>
#include <vector>   //牛客要包含vector

using namespace std;

class Solution{
public:
    Solution();
    void coordinate();
    void output();

private:
    vector<string> str;
    vector<int> flag = {1};
    vector<int> upper = {0};
    int x = 0;  //? 整型范围怎么计算
    int y = 0;
    // int _x = 0;
    // int _y = 0;

};

int main() {
    Solution s;
    s.coordinate();
    s.output();
    return 0;
}
// 64 位输出请用 printf("%lld")

Solution::Solution(){
    //flag.push_back(1);  //1表示有效坐标
    char c = '\0';
    int i = 0;
    string tem;
    while((c = getchar())!='\n')
        if(c!='\73') {//转义字符八进制至多三位数字表示 ? 运算顺序是什么
            if(!(('0'<=c&&c<='9')||('A'<=c&&c<='Z')))
                flag[i] =0;
            if('A'<=c&&c<='Z')
                upper[i]++;
            tem+=c;
        }
        else{
            str.push_back(tem); //数组中加入了空串
            flag.push_back(1);
            upper.push_back(0);
            i++;
            tem = "";
        }

        // if(('0'<=s[i]&&s[i]<='9')&&('A'<=s[i]&&s[i]<='Z'))

}

void Solution::coordinate(){
    int size = str.size();
    int i = 0;
    for(const string &s:str)
        if(s.size()>0&&('A'<=s[0]&&s[0]<='Z')&&flag[i]==1&&upper[i]==1){
            int n = atoi(s.substr(1).c_str());
            switch(s[0]){
                case 'A':x-=n;break;
                case 'D':x+=n;break;
                case 'S':y-=n;break;
                case 'W':y+=n;break;
            }
            i++;
        }
        else
            i++;
}

void Solution::output(){
    cout << x << "," << y;
}

全部评论

相关推荐

09-29 16:59
已编辑
门头沟学院 Java
牛客96609213...:疯狂背刺,之前还明确设置截止日期,还有笔试,现在一帮人卡在复筛,他反而一边开启扩招,还给扩招的免笔试,真服了,你好歹先把复筛中的给处理了再说
投递大疆等公司10个岗位
点赞 评论 收藏
分享
09-13 17:25
亲切的00后在笔试:我也遇到了,所以我早他一步查看图片
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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