首页 > 试题广场 >

迷路的牛牛

[编程题]迷路的牛牛
  • 热度指数: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
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)