HJ17 坐标移动

HJ17 坐标移动,第1张


c++:
#include
#include
#include
using namespace std;

int main(){
    string s;
    vector<string>temp;
    while(cin >> s){
        int sublen = 0;
        for(int i = 0; i < s.size(); i++){
            sublen++;
            if(s[i] == ';'){
                sublen -= 1;//子串多加了;的长度,所以这里要减去1
                temp.push_back(s.substr(i-sublen,sublen));//把子串依次放入vector
                sublen = 0;
            }
        }
        int num = 0;
        int x = 0, y = 0;
        for(int i = 0; i < temp.size(); i++){
            if(temp[i].size() == 3 && isdigit(temp[i][1]) && isdigit(temp[i][2])){//A10的情况
                num = (temp[i][1]-'0')*10 + (temp[i][2]-'0');
            }else if(temp[i].size() == 2 && isdigit(temp[i][1])){//A1的情况
                num = temp[i][1]-'0';
            }else{
                num = 0;
            }
            
            switch(temp[i][0]){
                case 'A':x -= num;//左
                    break;
                case 'D':x += num;//右
                    break;
                case 'W':y += num;//上
                    break;
                case 'S':y -= num;//下
            }
        }
        cout << x << ',' << y<<endl;
        return 0;
    }
}
python:
s = input()
subs = s.split(';')
# print(subs)
x = 0
y = 0
for c in subs:
    if not 2<=len(c)<=3:
        continue
    try:
        direction = c[0]
        step = int(c[1:])
        if direction in ['A','W','S','D']:
            if direction == 'A':
                x -= step
            elif direction == 'D':
                x += step
            elif direction == 'S':
                y -= step
            elif direction == 'W':
                y += step
    except:
        continue
print(str(x)+','+str(y))
        

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/567656.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-09
下一篇 2022-04-09

发表评论

登录后才能评论

评论列表(0条)

保存