首页 > 试题广场 >

迷路的牛牛

[编程题]迷路的牛牛
  • 热度指数:537 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
牛牛去犇犇老师家补课,出门的时候面向北方,但是现在他迷路了。虽然他手里有一张地图,但是他需要知道自己面向哪个方向,请你帮帮他。

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


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

输入

3
LRR

输出

E
#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;
}
编辑于 2019-03-31 16:13:31 回复(0)
num = int(input())
string = input()
L_count = string.count('L')
R_count = num - L_count
result = 'NESW'
print(result[(R_count - L_count)%4])

发表于 2019-10-16 14:04:40 回复(0)
import java.util.Scanner; public class Main { public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in); int i = scanner.nextInt();
        String next = scanner.next(); char[] dict={'W','E','S','N'}; char[] chars = next.toCharArray(); //System.out.println(i);  //System.out.println(chars);  int a=0; for (int j = 0; j < chars.length; j++) { if (chars[j] == 'L') {
                a-=1;
            } else {
                a+=1;
            }
        }
        System.out.println(a); if (a%4 <0) {
            System.out.println(dict[(a%4)+4]);
        }else {
            System.out.println(dict[(a%4)]);
        } //     }
}
发表于 2019-08-27 01:24:06 回复(0)
import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class FindDirections {  public static void main(String[] args) {
        Map<Integer,String> map = new HashMap<>();    map.put(0,"N");  map.put(-1,"E");  map.put(-2,"S");  map.put(-3,"W");  map.put(1,"W");  map.put(2,"S");  map.put(3,"E");   Scanner sc = new Scanner(System.in);  String turnTimes = sc.nextLine();  String turnDirect = sc.nextLine();  char[] directionTimes = turnDirect.toCharArray();  int countL = 0;  int countR = 0;  // System.out.println(directionTimes[0] == 'L');  for(int i = 0 ; i <= directionTimes.length - 1 ; i++) { if (directionTimes[i] == 'L')
                countL++;  else  countR++;   } int dist = (countL - countR)%4;  System.out.println(map.get(dist));    }
}

编辑于 2019-08-02 13:02:09 回复(0)
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Main {
    
    public static int RNum = 0;
    public static int LNum = 0;
    public static Map<Integer,Character> m = new HashMap();


    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();

        m.put(1,'E');
        m.put(2,'S');
        m.put(3,'W');
        m.put(0,'N');
        m.put(-1,'W');
        m.put(-2,'S');
        m.put(-3,'E');

        String str = sc.next();
        char[] ch = str.toCharArray();

        for (int i = 0;i< ch.length;i++){
            if(ch[i] == 'R'){
                RNum++;
            }
            else {
                LNum++;
            }
        }

        int temp = (RNum - LNum) % 4 ;

        System.out.println(m.get(temp));
    }
}

发表于 2018-09-05 11:57:39 回复(0)
print('NWSE'[(int(input())-2*input().count('R'))%4])
#Python取模用的除法默认用向下而不是向0取整所以这里取模可以保证不越界

编辑于 2018-07-03 21:24:55 回复(0)
function main(){
    var n = readline();//获取转方向的次数
    var str = readline();//获取转的方向
    var arr = str.split("");
    var position = ['N','E','S','W'];
    var count = 0;
    for(var i=0; i<n; i++){
        if(arr[i] === 'L' && count === 0){
            count = 3;
        }else if(arr[i] === 'L'){
            count --;
        }else if(arr[i] === 'R' && count === 3){
            count = 0;
        }else if(arr[i] === 'R'){
            count ++;
        }
    }
    console.log(position[count]);
}
main();

发表于 2018-06-21 17:34:13 回复(0)
import java.util.*;
public class Main { public static void main(String[] args) {

        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
         sc.nextLine(); 
        //Scanner sb=new Scanner(System.in);
        String s=sc.nextLine();
        char[] arr=s.toCharArray();
            int indexL=0;
        int indexR=0;
        char dirct=0;
        for(int i=0;i<arr.length;i++){
            
            if(arr[i]=='L'){
                indexL++;
            }else{
                indexR++;
            }
        }
        if(indexL>indexR){
            int num=(indexL-indexR)%4;
            switch(num){
            case 0:dirct= 'N'; break;
            case 1:dirct= 'W'; break;
            case 2:dirct= 'S'; break;
            case 3:dirct= 'E'; break;
            }
        }else{
                int num1=(indexR-indexL)%4;
                switch(num1){
                case 0:dirct= 'N'; break;
                case 1:dirct= 'E'; break;
                case 2:dirct= 'S'; break;
                case 3:dirct= 'W'; break;
            }
        
        }
        
        System.out.println(dirct);
    }

}

发表于 2018-06-20 22:57:04 回复(0)