每个输入包含一个测试用例。
每个测试用例的第一行包含一个正整数,表示转方向的次数N(N<=1000)。
接下来的一行包含一个长度为N的字符串,由L和R组成,L表示向左转,R表示向右转。
输出牛牛最后面向的方向,N表示北,S表示南,E表示东,W表示西。
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]);
}
} 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);
}
}
不知道有没有人用链表
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);
}
}
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();
}
} 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;
}
}
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);
}
} 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);
}
} 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");
}
}
} 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;
}
}
} ; 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); } }
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]);
}
}
} 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]);
}
} 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]);
}
} 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;
}
}
} 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]);
}
}
} 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()就可以
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取余