题解 | #坐标移动#

坐标移动

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

#include <iostream>
#include <bits/stdc++.h>
#include <string>
using namespace std;

int dis = 0;

int strvalid(string str){
    int cifang = 1;
    if(str[0] == 'W' ||  str[0] == 'A' || str[0] == 'S' || str[0] == 'D'){
        for(int i = str.size() - 1; i > 0; i--){
            if(str[i] > '9' || str[i] < '0') {
                dis = 0;
                return 0;
            }
            else{
                dis += (str[i] - '0')*cifang;
                cifang *= 10;
            }
        }
        return 1;
    }
    return 0;

}

int main() {
    int x = 0;
    int y = 0;
    string str;
    while(getline(cin, str,';')){
        dis = 0;
        // if(cin.get() == '\n') break;
        if(strvalid(str)){
            switch(str[0]){
                case 'W' : y += dis; break;
                case 'A' : x -= dis; break;
                case 'S' : y -= dis; break;
                case 'D' : x += dis; break;
            }
        }
    }
    cout << x << ',' << y << endl;
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

07-22 13:50
门头沟学院 Java
仁者伍敌:其实能找到就很好了,当然收支能抵
点赞 评论 收藏
分享
06-01 21:50
已编辑
天津理工大学 Java
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-21 13:38
8月实习会变多吗现在还没找到实习该怎么办...回复的hr好少
码农索隆:3-4月就要开始找,基本上6月份就发offer,7月初已经开始暑期实习了。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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