首页 > 试题广场 >

迷路的牛牛

[编程题]迷路的牛牛
  • 热度指数:47797 时间限制: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.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        int n=Integer.parseInt(br.readLine());
        String str=br.readLine();
        String[] dir={"N","W","S","E"};
        int count=0;
        for (int i = 0; i < n; i++) {
            if(str.charAt(i)=='L') count++;
        }
        int right=n-count;
        System.out.println(dir[((count-right)%4+4)%4]);
    }
}

发表于 2022-08-15 16:12:16 回复(0)

import java.util.Scanner;

public class Main{
    public static void main(String[] args) {
        char[] direction = {'N','E','S','W'};
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        String turn = input.next();
        int count = 0;
        for(int i=0;i<n;i++) {
            if(turn.charAt(i)=='L') {
                count--;
            }else if(turn.charAt(i)=='R') {
                count++;
            }
        }
        if(count>0) {
            System.out.println(direction[count%4]);
        }else if(count%4==0) {
            System.out.println("N");
        }else if(count<0) {
            System.out.println(direction[4+(count%4)]);
        }
    }
}
模系统的简单应用
发表于 2020-06-01 18:50:51 回复(0)
public class Demo{ 
    
    
    class FangXiang{
        String fangXiang ;
        FangXiang pre;
        FangXiang next;
        
        public FangXiang(String fangXiang){
            this.fangXiang = fangXiang;
        }

    }       
        public void step(String direction, FangXiang temp ,int num){
              String[] directions = direction.split("");
              for(int i=0;i<num;i++){
                  if(directions[i].equals("L")){
                      temp = temp.pre;
                  }else{
                      temp = temp.next;
                  }
              }
            System.out.println(temp.fangXiang); 
        }

    public static void main(String[] args){
        Demo d=new Demo();
        FangXiang N = d.new FangXiang("N");
        FangXiang S = d.new FangXiang("S");
        FangXiang W = d.new FangXiang("W");
        FangXiang E = d.new FangXiang("E");
        FangXiang temp = N; 
        //北
        N.pre = W;
        N.next = E;
        //东
        E.pre = N;
        E.next = S;
        //西
        W.pre = S;
        W.next = N;
        //南
        S.pre = W;
        S.next = E;
        
        d.step("L",temp,1);
    }

}
不知道有没有人用链表
发表于 2020-05-14 00:40:36 回复(0)
import java.util.Scanner;

/**
 * 每个输入包含一个测试用例。
 * 每个测试用例的第一行包含一个正整数,表示转方向的次数N(N<=1000)。
 * 接下来的一行包含一个长度为N的字符串,由L和R组成,L表示向左转,R表示向右转。
 * 输出描述:输出牛牛最后面向的方向,N表示北,S表示南,E表示东,W表示西。
 * 示例
 * 输入:
 * 3
 * LRR
 * 输出:
 * E
 */
public class 迷路的牛牛 {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int turns = s.nextInt();
        String direction = s.next();
        int center = 1;
        //n w s e
        for (int i = 0; i < turns; i++) {
            if('L&(6336)#39;==direction.charAt(i)){
                switch (center){
                    case 1:
                        center = 2;
                        break;
                    case 2:
                        center = 3;
                        break;
                    case 3:
                        center = 4;
                        break;
                    case 4:
                        center = 1;
                        break;
                }
            }else if('R&(5134)#39;==direction.charAt(i)){
                switch (center){
                    case 1:
                        center = 4;
                        break;
                    case 4:
                        center = 3;
                        break;
                    case 3:
                        center = 2;
                        break;
                    case 2:
                        center = 1;
                        break;
                }
            }
        }
        String location = null;
        switch (center){
            case 1:
                location = "N";
                break;
            case 2:
                location = "W";
                break;
            case 3:
                location = "S";
                break;
            case 4:
                location = "E";
                break;
        }
        System.out.println(location);
    }
}
发表于 2020-05-10 22:31:45 回复(0)
import java.util.Scanner;


public class FindDerection {

    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        String str = sc.next();
        int[] dp = new int[n+1];
        if(n == 0){
            System.out.println("N");
        }
        dp[0] = 0;
        char[] direction = str.toCharArray();
        for(int j = 0;j < direction.length;j++){
            if(direction[j] == 'L'){
                dp[j+1] = (dp[j] + 3) % 4;
            }else{
                dp[j+1] = (dp[j] + 1) % 4;
            }
        }
        switch(dp[n]){
            case 0:
                System.out.println("N");
                break;
            case 1:
                System.out.println("E");
                break;
            case 2:
                System.out.println("S");
                break;
            case 3:
                System.out.println("W");
                break;
        }


        sc.close();

    }

}


发表于 2020-04-28 09:30:39 回复(0)
import java.util.*;
public class Test {
        public static void main(String[] args){
            Scanner input = new Scanner(System.in);
            int num = Integer.parseInt(input.nextLine());
            String str = input.nextLine();
            char[] values = str.toCharArray();
            char flag = 'N';
            int left = 0;
            int right = 0;
            for(int i=0;i<values.length;i++){
                if(values[i]=='L'){
                    left++;
                }
                if(values[i]=='R'){
                    right++;
                }
            }
            int retain = 0;
            if(left>=right){
                retain = left-right;
                int end1 = retain%4;
                int end11 = end1%4;
                if(end11==1){
                    System.out.println("W");
                }else if(end11==2){
                    System.out.println("S");
                }else if(end11==3){
                    System.out.println("E");
                }else{
                    System.out.println("N");
                }
                return;
            }else{
                retain = right-left;
                 int end2 = retain%4;
                 int end21 = end2%4;
                if(end21==1){
                    System.out.println("E");
                }else if(end21==2){
                    System.out.println("S");
                }else if(end21==3){
                    System.out.println("W");
                }else{
                    System.out.println("N");
                }
                return;
            }
            
        }
        
    }

发表于 2020-04-17 18:19:54 回复(0)
import javax.sound.midi.Soundbank;
import java.util.Scanner;

/**
 * @author sunhongguang
 * @create 2020-04-14-19:46
 */
public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        //接收一个正数n
        int n = scanner.nextInt();
        String str = scanner.next();
        //得到最后牛牛所在的方向
        char direction = getDirection(str);
        System.out.println(direction);
    }

    private static char getDirection(String str) {
        //把字符串转换成字符数组
        char[] ch = str.toCharArray();
        //得到向左转的总次数
        int l_Length = getNum(ch);
        //得到向右转的总次数
        int r_length = ch.length - l_Length;
        //定义方向数组
        char[] directionArr = new char[]{'N','W','S','E'};
      
        //得到左转数量和右转数量的差值
        int num = l_Length - r_length;
        //如果相减结果为偶数,则最终方向就是相减的结果 % 4,得到的结果就是方向数组的下标
        if(Math.abs(num) % 2 == 0){
            return directionArr[Math.abs(num) % 4];
        }else{//如果num不为偶数,则要进一步判断num的正负
            if(num < 0){//如果num的值小于0,则说明左转次数比右转次数少,则对应方向数组的下标求法如下
                return directionArr[(Math.abs(num % 4) + 2) % 4];
            }else{//如果num的值不为偶数,且num大于0,则说明左转次数比右转次数多,对应方向数组的下标求法如下
                return directionArr[num % 4];
            }
        }
    }

    private static int getNum(char[] ch) {
        int l_length = 0;
        for(int i=0;i<ch.length;i++){
            if(ch[i] == 'L' || ch[i] == 'l'){
                l_length++;
            }
        }
        return l_length;
    }
}

编辑于 2020-04-14 21:48:45 回复(0)
import java.util.Scanner;

public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        String path = sc.next();

        //左转右转
        int dic = 0;
        for (int i = 0; i < n; i++) {
            if (path.charAt(i) == 'L'){
                dic--;
            }else {
                dic++;
            }
            if (dic==4 || dic==-4){
                dic=0;
            }
        }
        //最后朝哪个方向
        String s = "";
        if(dic==-1 || dic==3){
            s="W";
        }else if(dic==1 || dic==-3){
            s="E";
        }else if(dic==-2 || dic==2){
            s="S";
        }else {
            s="N";
        }
        System.out.println(s);
    }
}

发表于 2020-04-12 11:19:12 回复(0)
java
import java.util.Scanner;
public class Main{
    public static char position(int N,String s){
        int l_num=0,r_num=0;
        String pos1 = "NESW";
        String pos2 = "NWSE";
        for(int i=0;i<N;i++){
            if(s.charAt(i)=='L') l_num++;
            if(s.charAt(i)=='R') r_num++;
        }
        int diff=r_num-l_num;
        if(diff%4==0) return 'N';
        else if(diff>0){
            return pos1.charAt(diff%4);
        }
        else{
            int k=-diff;
            return pos2.charAt(k%4);
        }
    }
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        int N = input.nextInt();
        String str = input.next();
        char s = position(N,str);
        System.out.println(s);
    }
}


发表于 2020-03-14 22:55:50 回复(0)
import java.io.*;

public class Main{
    public static void main(String[] args) throws IOException{
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        int count = 0;
        String str1 = bf.readLine();
        String str2 = bf.readLine();
        for(int i = 0;i < str2.length();i++){
            if(str2.charAt(i) == 'L'){
                count--;
            }else{
                count++;
            }
        }
        if(count % 4 == 0){
            System.out.println("N");
        }
        if((count - 1) % 4 == 0){
            System.out.println("E");
        }
        if((count - 2) % 4 == 0){
            System.out.println("S");
        }
        if((count - 3) % 4 == 0){
            System.out.println("W");
        }
    }
}
ps:第一行的输入完全没必要啊!
发表于 2020-03-14 19:59:22 回复(0)

import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        int N=sc.nextInt();
        int r=0;
        String s=sc.next();
        for(int i=0;i<N;i++){
            if(s.charAt(i)=='L'){
                r++;
            }
        }
        int sum=((N-2*r)+(4*N))%4;
        switch(sum){
            case 0:
                System.out.print("N");
                break;
            case 1:
                System.out.print("E");
                break;
            case 2:
                System.out.print("S");
                break;
            case 3:
                System.out.print("W");
                break;
        }
    }
}

发表于 2020-02-24 18:34:40 回复(0)
;  import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class Main{ public static void direct(int Sum)
    { if (Sum>=0)
        { if(Sum%4==0) System.out.println("N");  if(Sum%4==1) System.out.println("E");  if(Sum%4==2) System.out.println("S");  if(Sum%4==3) System.out.println("W");   } else  {
            Sum = -Sum;  int k = Sum/4;  Sum = (k+1)*4 - Sum;  direct(Sum);  }
    } public static void main(String[] args) throws IOException { BufferedReader buff = new BufferedReader(new InputStreamReader(System.in));  int sum = 0;  int n = Integer.valueOf(buff.readLine());  String s = buff.readLine();  for(int i=0;i<n;i++)
        { if(s.charAt(i)=='R') sum++;  else if (s.charAt(i)=='L') sum--;  } direct(sum);  }
    }
发表于 2020-02-21 19:19:58 回复(0)
import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        char[] dir = {'N','W','S','E'};
        while(sc.hasNext()){
            int n = sc.nextInt();
            String str = sc.next();
            int left = 0;
            for(int i = 0; i < str.length(); i++){
                if(str.charAt(i) == 'L'){
                    left++;
                }else{
                    left--;
                }
            }
            while(left < 0){
                left += 4;
            }
            System.out.println(dir[left % 4]);
        }
    }
}

发表于 2020-02-19 20:31:46 回复(0)
import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner in=new Scanner(System.in);
        int num=in.nextInt();
        String str=in.next();
        String []W=new String[3];
        String []N=new String[3];
        String []E=new String[3];
        String []S=new String[3];
        for(int i=0;i<3;i++){
            if(i==0){ W[i]="W";N[i]="N";E[i]="E";S[i]="S"; }
            if(i==1){ W[i]="S";N[i]="W";E[i]="N";S[i]="E"; }
            if(i==2){ W[i]="N";N[i]="E";E[i]="S";S[i]="W"; }
        }
        String me="N";
        for(int i=0;i<num;i++){
            if(str.charAt(i)=='L'){
                if(me.equals("E")) {me=E[1];continue;}
                if(me.equals("W")) {me=W[1];continue;}
                if(me.equals("N")) {me=N[1];continue;}
                if(me.equals("S")) {me=S[1];continue;}
            }
            if(str.charAt(i)=='R'){
                if(me.equals("E")) {me=E[2];continue;}
                if(me.equals("W")) {me=W[2];continue;}
                if(me.equals("N")) {me=N[2];continue;}
                if(me.equals("S")) {me=S[2];continue;}
            }
        }
        System.out.println(me);
    }
}
发表于 2020-02-19 18:16:53 回复(0)
import java.util.Scanner;
public class Main {

    public  static  void  main(String[]  args)
    {
        Scanner  scanner=new Scanner(System.in);
        int num=scanner.nextInt();
        scanner.nextLine();
        String  str=scanner.nextLine();
        int  LNum=0,RNum=0;
        for(int i=0;i<str.length();i++){//遍历字符串进行计数
            if (str.charAt(i) == 'L') LNum++;
            else RNum++;
        }
        int distance=(LNum-RNum)%4;//做减法抵消
        int result=distance>0?4-distance:-distance;//4-distance是统一到顺时针
        String[]  directions={"N","E","S","W"};
        System.out.println(directions[result]);
    }
}


发表于 2019-12-23 22:20:35 回复(0)
看图说话
import java.util.*;
public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner cin=new Scanner (System.in);
		int n=cin.nextInt();
		String str=cin.next();
		String str1[]= {"E","S","W","N","E","S","W"};
		int d=0;
		for(int i=0;i<n;i++) {
			if(str.charAt(i)=='L') {
				if((d-1)==-4)d=0;
				else d--;
				
			}
			else if(str.charAt(i)=='R') {
				if((d+1)==4)d=0;
				else d++;
			}
		}
		System.out.print(str1[d+3]);

	}

}

发表于 2019-11-01 16:46:01 回复(0)
import java.util.Scanner;
public class Main{
    static int currentDirection = 0; // 0北 , 1东, 2/-2南, -1西
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        scan.nextLine();
        
        // 如果是有空格的,那么一次读完再拆字符串,和一个个读哪个快?
        String[] strArr = scan.nextLine().split("");
        for(String str : strArr){
            // switch和if哪个快?
            switch(str){
                case "R":
                    currentDirection = (currentDirection + 1 > 2 ? -1 : currentDirection + 1); 
                    break;
                case "L":
                    currentDirection = (currentDirection - 1 < -2 ? 1 : currentDirection - 1);
                    break;
                // 规范要写default
            }
        }
        scan.close();
        System.out.print(printDirection());
    }
    public static String printDirection(){
        switch(currentDirection){
            case 0:
                return "N";
            case 1:
                return "E";
            case -1:
                return "W";
            case 2:
            case -2:
                return "S";
            default:
            	return null;
        }
    }
}

发表于 2019-10-02 21:47:06 回复(0)
import java.util.*;
public class Main{
    public static void main(String[] args){
        char[] direction = {'N' , 'E' , 'S' , 'W'};
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        String roate = sc.next();
        int leftTime = 0;
        int rightTime = 0;
        //统计LR的个数
        for(int i = 0; i < N; i++){
            if(roate.charAt(i) == 'L'){
                leftTime++;
            }else if(roate.charAt(i) == 'R'){
                rightTime++;
            }
        }
        //根据左右转的次数找规律
        if(leftTime >= rightTime){
            if((leftTime - rightTime)%4 == 0){
                System.out.println(direction[0]);
            }else{
                System.out.println(direction[4 - ((leftTime - rightTime)%4)]);
            }
        }else{
            System.out.println(direction[(rightTime - leftTime)%4]);
        }
    }
}

发表于 2019-09-02 15:48:26 回复(0)
import java.util.Scanner;
public class Main{
    public static void main(String []args){
        char[] directs={'N','W','S','E'};
        int flag=0;

        Scanner sr=new Scanner(System.in);
        int total=sr.nextInt();
        String action=sr.next();

        for(int i=0;i<total;i++){
            if(action.charAt(i)=='L'){
                flag++;
            }else if(action.charAt(i)=='R'){
                flag--;
            }
        }
        flag=flag%4;
        if(flag<0){
            flag=flag+4;
        }
        System.out.println(directs[flag]);
    }
}

我真是nextLine()为什么不行next()就可以

发表于 2019-08-28 17:26:33 回复(0)
public class 牛牛去犇犇老师家补课 { public static void main(String[] args) {
        Scanner in = new Scanner(System.in); int n = in.nextInt();
        String s = in.next();
        String temp = s.replaceAll("R", ""); int r = s.length() - temp.length();//表示输入R的个数  int l = temp.length() * (-1); int sum = r + l; switch (sum % 4) { case 1:
                System.out.println("E"); break; case -3:
                System.out.println("E"); break; case -2:
                System.out.println("S"); break; case 2 :
                System.out.println("S"); break; case -1:
                System.out.println("W"); break; case 3 :
                System.out.println("W"); break; default:
                System.out.println("N"); break;
        }
    }
} 
分析:向左转为-1 向右转为1 
 将输入的转向字符串的R替换为空串,再用原来的字符长度串减去替换后的字符长度,得到的就是R的个数。
 R的个数*(-1), 然后加上L的个数,得到的sum就是转向的最终结果.最后和4取余

发表于 2019-08-11 17:52:06 回复(0)