华为机试题 坐标移动

题目描述

开发一个坐标计算工具, A表示向左移动,D表示向右移动,W表示向上移动,S表示向下移动。从(0,0)点开始移动,从输入字符串里面读取一些坐标,并将最终输入结果输出到输出文件里面。

 

输入:

 

合法坐标为A(或者D或者W或者S) + 数字(两位以内)

 

坐标之间以;分隔。

 

非法坐标点需要进行丢弃。如AA10;  A1A;  $%$;  YAD; 等。

 

下面是一个简单的例子 如:

 

A10;S20;W10;D30;X;A1A;B10A11;;A10;

 

处理过程:

 

起点(0,0)

 

+   A10   =  (-10,0)

 

+   S20   =  (-10,-20)

 

+   W10  =  (-10,-10)

 

+   D30  =  (20,-10)

 

+   x    =  无效

 

+   A1A   =  无效

 

+   B10A11   =  无效

 

+  一个空 不影响

 

+   A10  =  (10,-10)

 

 

 

结果 (10, -10)



输入描述:

一行字符串



输出描述:

最终坐标,以,分隔


输入例子:
A10;S20;W10;D30;X;A1A;B10A11;;A10;

输出例子:
10,-10


该题先预处理一下,取得所有的指令的列表,再一次处理会比较简单

然后根据题意,正常的指令要么是3个字符A10   要么是2个字符  A1

所以根据指令的长度先排除一些错误指令,然后根据指令的特点继续排除和处理指令

这道题需要注意是要考虑处理多行指令,所以最外层要有while循环

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include<iostream>
#include<vector>
 
using namespace std;
 
int main(){
    string s;
    while(getline(cin,s))
    {
        int heng=0;
        int zong=0;
        string temp;
        vector<string> vs;
        temp.clear();
        for(int i=0;i<s.size();i++){
            if(s[i]==';'){
                vs.push_back(temp);
                temp.clear();
            }
            else{
                temp.push_back(s[i]);
            }
        }
        for(int i=0;i<vs.size();i++)
        {
            if(vs[i].size()==2 || vs[i].size()==3) 
            {
                if(vs[i][0]=='A' || vs[i][0]=='W' || vs[i][0]=='S' || vs[i][0]=='D')
                {
                    if(vs[i].size()==2){
                        if(vs[i][1]>='0' && vs[i][1]<='9')
                        {
                            if(vs[i][0]=='A')
                                heng=heng-(vs[i][1]-'0');
                            else if(vs[i][0]=='D')
                                heng=heng+(vs[i][1]-'0');
                            else if(vs[i][0]=='W')
                                zong=zong+(vs[i][1]-'0');
                            else if(vs[i][0]=='S')
                                zong=zong-(vs[i][1]-'0');  
                        }
                        else
                            continue;
                    }
                    else if(vs[i].size()==3){
                        if(vs[i][1]>='0' && vs[i][1]<='9' && vs[i][2]>='0' && vs[i][2]<='9')
                        {
                            if(vs[i][0]=='A')
                                heng=heng-(vs[i][1]-'0')*10-(vs[i][2]-'0');
                            else if(vs[i][0]=='D')
                                heng=heng+(vs[i][1]-'0')*10+(vs[i][2]-'0');
                            else if(vs[i][0]=='W')
                                zong=zong+(vs[i][1]-'0')*10+(vs[i][2]-'0');
                            else if(vs[i][0]=='S')
                                zong=zong-(vs[i][1]-'0')*10-(vs[i][2]-'0');
                        }
                        else
                            continue;
                    }
                }
                else
                    continue;
            }
            else
                continue;
        }
        cout<<heng<<","<<zong<<endl;
        s.clear();
    }
}





posted @ 2016-09-02 21:03  gremount  阅读(434)  评论(0编辑  收藏  举报