牛客华为机试题

题目描述
开发一个坐标计算工具, 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)

输入描述:
一行字符串

输出描述:
最终坐标,以,分隔

示例1
输入
复制
A10;S20;W10;D30;X;A1A;B10A11;;A10;
输出
复制
10,-10

CodeBlocks测试代码

#include
#include
#include
using namespace std;
int main()
{
    string str;
    cin>>str;
    int x=0,y=0;
    vector<string> v;
    string tmpstr="";
    for(int i=0; i<str.length();i++)//去除字符串中的';'  并将单词存进vector动态数组
    {
        if(str[i]!=';')
        {
            tmpstr.push_back(str[i]);
        }
        else if(tmpstr!="")
        {
            v.push_back(tmpstr);
            tmpstr="";
        }
    }
    v.push_back(tmpstr);
    vector<string>::iterator it;
    for(it=v.begin();it!=v.end();)
    {
        string tmpit=*it;
        if(tmpit.size()==2&&tmpit[1]>='0'&&tmpit[1]<='9')//针对2位字符的单词的处理情况
        {
            if(tmpit[0]=='A')
            {
                stringstream strstream;
                //使用stringstream流处理字符串string转换成整型int,下同
                strstream<<tmpit[1];
                int value;
                strstream>>value;
                x-=value;
                it++;
            }
            else if(tmpit[0]=='D')
            {
                stringstream strstream;
                strstream<<tmpit[1];
                int value;
                strstream>>value;
                x+=value;
                it++;
            }
            else if(tmpit[0]=='W')
            {
                stringstream strstream;
                strstream<<tmpit[1];
                int value;
                strstream>>value;
                y+=value;
                it++;
            }
            else if(tmpit[0]=='S')
            {
                stringstream strstream;
                strstream<<tmpit[1];
                int value;
                strstream>>value;
                y-=value;
                it++;
            }
            else it++;
        }
        else if(tmpit.size()==3&&tmpit[1]>='0'&&tmpit[1]<='9'&&tmpit[2]>='0'&&tmpit[2]<='9')//针对3位字符的单词的处理情况
        {
            if(tmpit[0]=='A')
            {
                stringstream strstream;
                strstream<<tmpit[1];
                strstream<<tmpit[2];
                int value;
                strstream>>value;
                x-=value;
                it++;
            }
            else if(tmpit[0]=='D')
            {
                stringstream strstream;
                strstream<<tmpit[1];
                strstream<<tmpit[2];
                int value;
                strstream>>value;
                x+=value;
                it++;
            }
            else if(tmpit[0]=='W')
            {
                stringstream strstream;
                strstream<<tmpit[1];
                strstream<<tmpit[2];
                int value;
                strstream>>value;
                y+=value;
                it++;
            }
            else if(tmpit[0]=='S')
            {
                stringstream strstream;
                strstream<<tmpit[1];
                strstream<<tmpit[2];
                int value;
                strstream>>value;
                y-=value;
                it++;
            }
            else it++;
        }
        else{it++;}//其它单词均是无效单词,不处理
    }
    cout<<x<<","<<y;
    return 0;
}

你可能感兴趣的:(牛客华为机试题)