华为机试题--16.坐标移动

题目描述

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

输入:

  • 合法坐标为A(或者D或者W或者S) + 数字(两位以内)
  • 坐标之间以;分隔。
  • 非法坐标点需要进行丢弃。如AA10; A1A; <script type="math/tex" id="MathJax-Element-40">%</script>; 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

知识点:

string转int

std::string str = "123";
int n = atoi(str.c_str());

int转string

int n = 123;
std::stringstream ss;
std::string str;
ss<<n;
ss>>str;

代码:

#include<iostream>
#include<vector>
#include<string>
#include<algorithm>

using namespace std;

int main()
{
    vector<string> input;
    string tmp;

    while (getline(cin, tmp))
    {
        //处理输入的字符串
        //以;分割每个内容,并存入到vector<string> input中
        auto p1 = tmp.begin();
        auto index = find(p1, tmp.end(), ';');
        while (index != tmp.end())
        {
            string i(p1, index);
            p1 = index + 1;
            input.push_back(i);
            index = find(p1, tmp.end(), ';');
        }

        //Debug  查看input中存放的内容
        //for (auto i : input)
        //  cout << i << endl;


        int x = 0, y = 0;
        //处理非法输入
        int opt = 0;
        for (auto i : input)
        {
            //仅当子字符串的开头为ADWS才继续处理
            if (i[0] == 'A' || i[0] == 'D' || i[0] == 'W' || i[0] == 'S'){
                //当子字符串长度==2,也即移动数字为1
                if (i.size() == 2)
                {
                    //仅当移动内容为数字时处理,其余的跳出本次循环
                    if (isdigit(i[1]))
                    {
                        //处理string到int。注意要将string转换成c风格字符串
                        string temp(i.begin() + 1, i.end());
                        opt = atoi(temp.c_str());
                        //cout << "2:::::" << opt;
                    }
                    else
                        continue;
                }
                //A10
                else if (i.size() == 3)
                {
                    if (isdigit(i[1]) && isdigit(i[2]))
                    {
                        string temp(i.begin() + 1, i.end());
                        opt = atoi(temp.c_str());
                        //cout << "3:::::" << opt;
                    }
                    else
                        continue;
                }
                else
                    continue;

                switch (i[0])
                {
                case 'A':x -= opt; break;
                case 'D':x += opt; break;
                case 'W':y += opt; break;
                case 'S':y -= opt; break;
                }
            }
            //Debug   查看每一步的移动结果
            //cout << x << "," << y << endl;
        }
        cout << x << "," << y << endl;
        input.clear();
    }
    return  0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值