网易笔试——迷路的牛牛

这道题很有意思,我自己是用比较笨的方法,看到网上好多人用的是把东西南北编码的方法,把东西南北画出来顺时针编码为0,1,2,3(对应北东南西),向右走相当于顺时针走,对应编码是(x+1+4)%4(加4取余是为了不出现负数);向左走相当于逆时针走,对应编码是对应编码是(x-1+4)%4

下面摘抄牛客网上的程序:

链接:https://www.nowcoder.com/questionTerminal/fc72d3493d7e4be883e931d507352a4a
来源:牛客网

#include
using namespace std;
int main()
{
     int n; cin >> n;
     char director[ 5 ] = "NESW" ;
     int now = 0 ;
     for ( int i = 0 ; i < n; i++) {
         char next; cin >> next;
         if (next == 'L' ) {
             now = (now + 4 - 1 ) % 4 ;
         }
         else {
             now = (now + 1 ) % 4 ;
         }
     }
     cout << director[now];
     return 0 ;
}

你可能感兴趣的:(笔记,网易笔试)