牛客网 坐标移动

#include<bits/stdc++.h>

using namespace std;

int main()
{
    //加快cin的效率
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    
    string s,t;
    while(getline(cin,s))
    {
        stringstream ss(s);
        pair<int,int> p(0,0);//初始值
        while(getline(ss,t,';'))
        {
            if(t.empty())
            {
                continue;
            }
            string _ = t.substr(1);
            if(regex_match(_, regex("[0-9]+")))
               {
                   switch (t[0])
                   {
                       case 'A':
                           p.first -= stoi(_);
                           break;
                       case 'D':
                           p.first += stoi(_);
                           break;
                       case 'W':
                           p.second += stoi(_);
                           break;
                       case 'S':
                           p.second -= stoi(_);
                           
                   }
               }
               }
        cout << p.first << "," << p.second << endl;
        }     
    return 0;
}

1、加快cin 的速度
ios::sync_with_stdio(false);
std::ios::sync_with_stdio(false);,这样就可以取消cin于stdin的同步了
cin.tie(0),cout.tie(0);
tie(0)(0表示NULL)来解除cin与cout的绑定,进一步加快执行效率
2、getline(ss,t,‘;’)
在这里插入图片描述
getline是从流中获取一行信息输入到字符串中:
从is中提取字符并将其存储到str中,直到找到定界字符delim(或换行字符“n”,对于(2))。
3、regex_match(,regex(“[0-9]*”) 需要更正为 regex_match(,regex(“[0-9]+”) 理由:[0-9]+ 表示必须出现一次数字, 而不是[0-9]* 不出现也可以,否则 string _ 会出现为空的情况,导致 stoi(_) 出错

参考连接:https://blog.nowcoder.net/n/f4bfcd09267147f3ac4d99b28d1d26be?f=comment

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值