网易2018 迷路的牛牛

牛牛去犇犇老师家补课,出门的时候面向北方,但是现在他迷路了。虽然他手里有一张地图,但是他需要知道自己面向哪个方向,请你帮帮他。

输入描述:

每个输入包含一个测试用例。
每个测试用例的第一行包含一个正整数,表示转方向的次数N(N<=1000)。
接下来的一行包含一个长度为N的字符串,由L和R组成,L表示向左转,R表示向右转。

输出描述:

输出牛牛最后面向的方向,N表示北,S表示南,E表示东,W表示西。

输入例子1:

3
LRR

输出例子1:

E
#include <bits/stdc++.h>
using namespace std;

char diretion(char &c,char &dire) {
	string status = "NESW";	
	int n = (c == 'L') ? -1 : 1;
	int pos = status.find(dire);
	return status[(pos + n + 4) % 4];
}

int main() {
	int n;
	//freopen("input.txt", "r", stdin);
	while (cin >> n) {
		string s;
		char dire = 'N';
		cin >> s;
		for (char c : s) {
			dire = diretion(c, dire);
		}
		cout << dire << endl;
	}
	return 0;
}

简化一点:

#include <bits/stdc++.h>
using namespace std;


int main() {
    int n;
    const string status = "NESW";
    while (cin >> n) {
        string s;
        cin >> s;
        int i = 0;
        for (char &c : s) {
            if (c == 'L')
                i--;
            else
                i++;
            if (i == -1 || i == 4)
                i = (i + 4) % 4;
        }        
        cout << status[i] << endl;
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值