1313: 机器人行走
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 13 Solved: 8
[ Submit][ Status][ Web Board]
Description
Input
Output
Sample Input
5
L100R50R10
3LLL5RR4L12
LL
100R
5L5L5L5
Sample Output
102.96
9.06
0.00
100.00
0.00
import java.util.*;
public class 机器人走路{
static int direction=1;
//direction 分别为 0左、1上、2右、3下
static int[] X= {-1,0,1,0};
static int[] Y= {0,1,0,-1};
static int x=0;
static int y=0;
//计算xy
public static void countXY(String ss) {
if(!ss.equals("")) {
int n=Integer.parseInt(ss);
x+=n*X[direction];
y+=n*Y[direction];
}
}
//修改方向
public static void alterDirection(char c) {
if(c=='L')
direction=(direction+3)%4;
else if(c=='R')
direction=(direction+1)%4;
}
//对字母和数字处理的函数
public static void f(String s) {
String ss="";
for(int i=0;i<s.length();i++) {
char c=s.charAt(i);
if(c>='0'&&c<='9') {
ss+=c;
}else {
countXY(ss);
ss="";
alterDirection(c);
}
}
countXY(ss); //for循环后查询是否有暂存的数字,重新计算
ss=""; //清空数字
}
public static void cal() {
System.out.printf("%.2f\n",Math.sqrt(x*x+y*y));
x=0;
y=0;
}
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
int n;
n=in.nextInt();
String[] s=new String[n];
for(int i=0;i<n;i++) {
s[i]=in.next();
f(s[i]);
cal();
}
}
}