华为笔试题:坐标移动

 

 

#include<string>
#include<iostream>
#include<algorithm>
#include<vector>
#include<set>
#include<functional>
using namespace std;
 
bool Valid(string A)
{
    if(A[0] == 'A' || A[0] == 'D' || A[0] == 'W' || A[0] == 'S')
    {
        if( A.length() <= 3 && A.length() >= 2 )
        {
            if(A.length() == 2)
            {
                if( A[1] <= '9' && A[1] >= '0' ) return true;
                else return false;
            }
             
            else
            {
                if( A[1] <= '9' && A[1] >= '0' && A[2] <= '9' && A[2] >= '0' ) return true;
                else return false;
            }
        }
         
        else return false;
         
    }
    else return false;
}
 
 
string MovePos(string A)
{
    int len = A.length(), x_axis = 0, y_axis = 0; 
    for(int i = 0; i < len;i++)
    {
        string tmp;  
        while(  A[i] != ';' )
        {
            tmp += A[i];
            i++;
        }
       if( Valid(tmp) )
       {
          if(tmp[0] == 'A' || tmp[0] == 'D') 
          {
               
              if(tmp.length() == 3)
              {
                  int offset = (tmp[1]-'0')*10 + (tmp[2]-'0');
                  tmp[0] == 'A' ? x_axis -= offset :  x_axis += offset;
              }
              else
              {
                  int offset = (tmp[1]-'0');
                  tmp[0] == 'A' ? x_axis -= offset :  x_axis += offset;
              }
          }
          else
          {
               if(tmp.length() == 3)
              {
                  int offset = (tmp[1]-'0')*10 + (tmp[2]-'0');
                  tmp[0] == 'S' ? y_axis -= offset :  y_axis += offset;
              }
              else
              {
                  int offset = (tmp[1]-'0');
                  tmp[0] == 'S' ? y_axis -= offset :  y_axis += offset;
              }            
          }      
       }                    
   }
     
    string res;
    res += to_string(x_axis);
    res += ',';
    res += to_string(y_axis);
    return  res;
}
 
 
int main()
{
    string n;
    while(cin>>n)
    {
    
    cout<<MovePos(n)<<endl;
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值