每个输入包含一个测试用例。
每个测试用例的第一行包含一个正整数,表示转方向的次数N(N<=1000)。
接下来的一行包含一个长度为N的字符串,由L和R组成,L表示向左转,R表示向右转。
输出牛牛最后面向的方向,N表示北,S表示南,E表示东,W表示西。
3 LRR
E
#include <bits/stdc++.h>
using namespace std;
int n, cnt = 1001;
char s[1010], ans[4];
int main() {
ans[1] = 'N', ans[2] = 'E', ans[3] = 'S', ans[0] = 'W';
cin >> n;
cin >> s;
for (int i = 0; i < n; ++i) {
if (s[i] == 'R') cnt++;
else cnt--;
}
cout << ans[cnt % 4];
} 简洁点的
#include <bits/stdc++.h>
int n, cnt = 1001;
char s[1010], ans[4] = {'W', 'N', 'E', 'S'};
int main() {
scanf("%d%s", &n, &s);
for (int i = 0; i < n; ++i) s[i] == 'R' ? cnt++ : cnt--;
printf("%c", ans[cnt % 4]);
}
#include<bits/stdc++.h>
using namespace std;
int main(){
int n; cin >> n;
int ans = 0;
char dir[] = "NESW";
string str; cin >> str;
for(int i = 0; i < n; i++)
ans = (ans + (str[i] == 'L' ? -1:1) + 4) % 4;
cout << dir[ans%4] << endl;
return 0;
}
从北开始,顺时针旋转,将N,E,S,W分别记为0,1,2,3,左转相当于逆时针转,即-1;右转+1。避免结果为负数加4再对4取余。
char[] direction = new char[] {'E', 'S', 'W', 'N', 'E', 'S', 'W'}; 然后分别计算Left和Right的步数,并将其%4,最后direction[3 + (countRight - countLeft)]就是所求方向。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
char[] direction = new char[] {'E', 'S', 'W', 'N', 'E', 'S', 'W'};
int n = sc.nextInt();
String path = sc.next();
int countLeft = 0;
int countRight = 0;
for (int i = 0; i < n; i++) {
if (path.charAt(i) == 'L') {
countLeft++;
} else {
countRight++;
}
}
countLeft %= 4;
countRight %= 4;
System.out.println(direction[3 + (countRight - countLeft)]);
}
}
本套8道题全部pass的C++代码已挂到了我的GitHub(https://github.com/shiqitao/NowCoder-Solutions)
牛客网上的其他题目解答也在持续更新。
#include <iostream>
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;
}
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
String[] dire = new String[]{"N", "E", "S", "W"};
int n = in.nextInt();
String s = in.next();
int ret = 0;
for(int i = 0; i < n; i ++){
if(s.charAt(i) == 'R'){
ret ++;
}else{
ret --;
}
}
ret %= 4;
if(ret < 0){
ret += 4;
}
System.out.println(dire[ret]);
}
}
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);
}
} 简单粗暴
#include<iostream>
#include<string>
using namespace std;
int main()
{
int n;
string b;
char t='N';
cin>>n;
cin>>b;
for(int i=0;i<n;i++)
{
switch(t)
{
case 'N':
switch(b[i])
{
case 'L':t='W';break;
case 'R':t='E';break;
};break;
case 'E':
switch(b[i])
{
case 'L':t='N';break;
case 'R':t='S';break;
};break;
case 'S':
switch(b[i])
{
case 'L':t='E';break;
case 'R':t='W';break;
};break;
case 'W':
switch(b[i])
{
case 'L':t='S';break;
case 'R':t='N';break;
};break;
}
}
cout<<t;
return 0;
}
import sys n = int(sys.stdin.readline().strip()) s = sys.stdin.readline().strip() res = 0 for i in range(n): if s[i] == "L": res -= 1 else: res += 1 res %= 4 direction = ['N','E','S','W'] print(direction[res])
#include<iostream>
(720)#include<string>
using namespace std;
int main() {
int n;
string str;
cin >> n >> str;
int l = 0;//L的个数
int r = 0;//R的个数
for (int i = 0; i < n; i++) {
str[i] == 'L' ? l++ : r++;
}
l %= 4;//4个L抵消
r %= 4;//4个R抵消
l = (l - r) >= 0 ? (l - r) : (l - r) + 4;//L与R合并,取正
switch (l) {
case 3:cout << "E"; break;
case 2:cout << "S"; break;
case 1:cout << "W"; break;
case 0:cout << "N"; break;
}
system("pause");
}
while True:
try:
times = int(input())
dircs = str(input())
count = 0
for i in range(times):
if dircs[i] == "L":
count += 1
if dircs[i] == "R":
count -= 1
if (count % 4 == 0):
print("N")
if (count % 2 == 0) and (count % 4 != 0):
print("S")
if (count % 4 == 1)&nbs***bsp;(count % 4 == 1):
print("W")
if (count % 4 == 3)&nbs***bsp;(count % 4 == 3):
print("E")
except:
break # 总共就只有4个方向,2中转向
# 统计左转和右转数量的差,就是最后左转比右转多了几次,或者右转比左转多了几次
# 相对多转的次数%4取余,因为左转7次和左转3次效果是一样的
# 最后根据余数枚举当前方向
n = int(input()) # 转向次数
s = input()
ln = s.count('L') # 左转次数
rn = n - ln # 右转次数
lr = (rn-ln)%4 + 3 # 取余之后范围在-3 ~3,+3之后范围0~6,方便下面的字符串取值
target = "ESWNESW"
print(target[lr]) //出门面朝北,let directionArr = ["N","E","S","W"]
//遍历turn, turn[i]='R' =>cnt++
//return directionArr[cnt取模]
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
})
let inArr = []
rl.on('line',line=>{
if(!line) return
inArr.push(line.trim())
if(inArr.length == 2){
let directionArr = ["N","E","S","W"],
turn = inArr[1],
cnt = 0
for (let i = 0; i < turn.length; i++) {
turn[i]=='R'? cnt++ :cnt--
}
// 负数取模:先化为正数取模再取反N-0 E-1 S-2 W-3
cnt = (cnt%4+4)%4;
console.log(directionArr[cnt%4])
}
}) 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;
}
}
} #include<iostream>
#include<cstring>
using namespace std;
int main(){
int n;
while(cin>>n){
char *ch=new char[n];
cin>>ch;
int left=0,right=0;
for(int i=0;i<n;i++){
if(ch[i]=='L')
left++;
else
right++;
}
if(right>left){
right-=left;
if(right%4==1)
cout<<"E"<<endl;
else if(right%4==2)
cout<<"S"<<endl;
else if(right%4==3)
cout<<"W"<<endl;
else
cout<<"N"<<endl;
}
else{
left-=right;
if(left%4==1)
cout<<"W"<<endl;
else if(left%4==2)
cout<<"S"<<endl;
else if(left%4==3)
cout<<"E"<<endl;
else
cout<<"N"<<endl;
}
}
return 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]);
}
}