62,614
社区成员
发帖
与我相关
我的任务
分享
public class Car extends Vehicle{
protected int speed;
private int direction;
private int x;
private int y;
public Car(){
reset();
}
public void reset(){
this.direction = 0;
this.x = 0;
this.y = 0;
}
public void turn(String sDirction){
if (sDirction.compareTo("R") == 0){
this.direction = (this.direction+1)%4;
}
else if (sDirction.compareTo("L") == 0){
this.direction = (this.direction+4-1)%4;
}
}
public void walk(int sDistance){
if (this.direction == 0){
this.y += sDistance;
}
else if (this.direction == 2){
this.y -= sDistance;
}
else if (this.direction == 1){
this.x += sDistance;
}
else if (this.direction == 3){
this.x -= sDistance;
}
}
public double range(){
return Math.sqrt(this.x*this.x + this.y*this.y);
}
public void execute(String cmds){
List<String> subCmds = new ArrayList<String>();
int lastPos = 0;
while (true)
{
int sLPos = cmds.indexOf("R", lastPos);
int sRPos = cmds.indexOf("L", lastPos);
int sPos = -1;
if (sLPos == -1){
sPos = sRPos;
}
else if (sRPos == -1)
{
sPos = sLPos;
}
else
{
sPos = sLPos > sRPos ? sRPos : sLPos;
}
if (sPos < 0)
{
if (!cmds.substring(lastPos).isEmpty()){
subCmds.add(cmds.substring(lastPos));
}
break;
}
String distance = cmds.substring(lastPos, sPos);
String cmd = cmds.substring(sPos, sPos+1);
if (!distance.isEmpty()){
subCmds.add(distance);
}
subCmds.add(cmd);
lastPos = sPos+1;
}
for (String s : subCmds){
if (s.compareTo("R") == 0 || s.compareTo("L") == 0){
turn(s);
}
else{
walk(Integer.valueOf(s));
}
}
}
public static void main(String[] args){
Car c = new Car();
c.execute("L100R50R10");
System.out.println(c.range());
c.reset();
c.execute("3LLL5RR4L12");
System.out.println(c.range());
c.reset();
c.execute("LL");
System.out.println(c.range());
c.reset();
c.execute("100R");
System.out.println(c.range());
c.reset();
c.execute("5L5L5L5");
System.out.println(c.range());
}
}
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Car
{
static final int NORTH = 0;
static final int SOUTH = 1;
static final int WEST = 2;
static final int EAST = 3;
static final int LEFT = 2;
static final int RIGHT = 3;
static final DecimalFormat DF = new DecimalFormat("0.00");
public int getDirection(int direction, int action)
{
if (direction == NORTH || direction == SOUTH)
{
return direction ^ action;
}
else
{
return direction ^ action ^ 1;
}
}
public void execute(String command)
{
String regex = "[L|R]{0,1}[0-9]*";
Pattern pt = Pattern.compile(regex);
Matcher mather = pt.matcher(command);
ArrayList<String> ar = new ArrayList<String>();
while (mather.find())
{
String s = mather.group();
ar.add(s);
}
// 下面初始原点坐标与方向
int direction = NORTH;
int x = 0;
int y = 0;
for (int i = 0; i < ar.size(); i++)
{
String order = ar.get(i);
int len = order.length();
if (len > 0)
{
char action = order.charAt(0);
int move = 0;
if (action == 'L')
{
direction = getDirection(direction, LEFT);
if (len > 1)
{
move = Integer.parseInt(order.substring(1));
}
}
else if (action == 'R')
{
direction = getDirection(direction, RIGHT);
if (len > 1)
{
move = Integer.parseInt(order.substring(1));
}
}
else
{
move = Integer.parseInt(order);
}
switch (direction)
{
case NORTH:
y += move;
break;
case SOUTH:
y -= move;
break;
case WEST:
x -= move;
break;
case EAST:
x += move;
break;
}
}
}
double d = Math.sqrt(x * x + y * y);
System.out.println("移动直线距离为: " + DF.format(d));
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int count = sc.nextInt();
if (count <= 0 || count > 100)
{
System.out.println("次数超出限制");
return;
}
ArrayList<String> ar = new ArrayList<String>();
while (count > 0)
{
String command = sc.next();
ar.add(command);
count--;
}
for (String command : ar)
{
Car c = new Car();
c.execute(command.trim().toUpperCase());
}
}
}
public static double getDistance(String code)
{
String mCode = code.toUpperCase().trim();
double distance;
// only direction
Pattern p = Pattern.compile("[0-9]+");
Matcher m = p.matcher(mCode);
ArrayList<String> alist = new ArrayList<String>();
while (m.find())
{
alist.add(m.group());
}
// record x and y distance from (0,0)
double x = 0;
double y = 0;
Testing t = new Testing();
// init direction as whatever you like
Direct d = t.new Direct(0, 1);
if (alist.size() > 0)
{
mCode = mCode
.substring(mCode.indexOf(alist.get(0)), mCode.length());
Iterator<String> it = alist.iterator();
while (it.hasNext())
{
// handle distance
String dis = it.next();
int disInt = Integer.parseInt(dis);
x += d.dx * disInt;
y += d.dy * disInt;
// handle direction change
if ((mCode.length() - dis.length()) > 0)
{
mCode = mCode.substring(dis.length(), mCode.length());
while (mCode.length() > 0
&& (mCode.startsWith("L") || mCode.startsWith("R")))
{
if (mCode.charAt(0) == 'L')
{
d.turnLeft();
}
else if (mCode.charAt(0) == 'R')
{
d.turnRight();
}
mCode = mCode.substring(1, mCode.length());
}
}
}
}
else
{
return 0.0; // only direction
}
distance = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
return distance;
}
/**
* Direction class
**/
class Direct
{
public int dx;
public int dy;
public Direct(int dx, int dy)
{
this.dx = dx;
this.dy = dy;
}
public void turnLeft()
{
if (dx == 0 && dy == 1)
{
dx = -1;
dy = 0;
}
else if (dx == -1 && dy == 0)
{
dx = 0;
dy = -1;
}
else if (dx == 0 && dy == -1)
{
dx = 1;
dy = 0;
}
else if (dx == 1 && dy == 0)
{
dx = 0;
dy = 1;
}
}
public void turnRight()
{
if (dx == 0 && dy == 1)
{
dx = 1;
dy = 0;
}
else if (dx == -1 && dy == 0)
{
dx = 0;
dy = 1;
}
else if (dx == 0 && dy == -1)
{
dx = -1;
dy = 0;
}
else if (dx == 1 && dy == 0)
{
dx = 0;
dy = -1;
}
}
}
public class MovingProcessor {
//所谓四面八方,把方向控制好就行了
//以2维坐标为参考,前为上(y坐标增加),右为右(x坐标增加),后为下(y坐标减少),左为左(x坐标减少)
private static final int[][] MOVING_DIR = {{0,1}, {1,0}, {0,-1}, {-1,0}};
protected int dir = 0; //用于控制方向
protected double x = 0; //用于控制x坐标
protected double y = 0; //用于控制y坐标
private boolean isolating = true; //是否是独立的命令(用于扩展,此题可忽略),因为是每次独立计算距离(即后面的距离不累加前面的距离),所以本题设为独立命令
//private boolean realTime = false;//是否是实时处理(用于扩展,此题忽略)
public MovingProcessor() {}
public MovingProcessor(boolean isolating) {
this.isolating = isolating;
}
public void reset() { //处理器重置
dir = 0;
x = 0;
y = 0;
}
public double execute(String cmd) throws Exception { //执行指令,支持小数距离(题目给的是整数距离,这里扩展支持小数距离)
if (isolating) reset();
int dec = 0; //小数标志
double num = 0, times = 0;
for (char c : cmd.toCharArray()) { //按字符遍历指令
if (c == 'L') { //遇到左转L指令
if (num > 0) {//判断是否L指令前是否存在移动距离,如果存在,则先移动
move(num); //移动距离
num = 0;
dec = 0;
}
changeDir(c); //改变方向
} else if (c == 'R') { //遇到R右转指令
if (num > 0) {
move(num);
num = 0;
dec = 0;
}
changeDir(c);
} else if (c>='0' && c<='9') { //遇到数字累加数字
if (dec == 0) { //无小数的时候
num = num*10 + (c-'0');
} else { //有小数的时候
num += (c-'0')/times;
times *= 10;
}
} else if (c == '.') { //遇到小数点
if (dec > 0) { //非法小数点则抛出异常
throw new Exception(String.format("Error Command[%s].", cmd));
}
dec = 1;
times = 10;
} else { //非法字符则抛出异常
throw new Exception(String.format("Error Command[%s].", cmd));
}
}
if (num > 0) { //如果有移动距离就移动
move(num);
}
return Math.sqrt(x*x + y*y); //返回移动的距离
}
protected void changeDir(char dir) { //改变方向
this.dir += (dir=='L' ? -1 : (dir=='R' ? 1 : 0));
}
protected void move(double value) { //移动距离
dir %= 4;
dir += (dir < 0) ? 4 : 0;
x += value * MOVING_DIR[dir][0];
y += value * MOVING_DIR[dir][1];
}
public static void main(String[] args) {
String[] cmds = getCmds();
MovingProcessor mp = new MovingProcessor();
System.out.println("Execute Result:");
for (String cmd : cmds) {
try {
System.out.printf("%.2f\n", mp.execute(cmd));
} catch (Throwable e) {
e.printStackTrace();
//break; //skip, ignore, cancel, so on...
}
}
}
public static String[] getCmds() {
String[] cmds;
int count = 0;
Scanner sc = new Scanner(System.in);
try {
System.out.println("User Input:");
count = Integer.valueOf(sc.nextLine()).intValue();
} catch (Throwable e) {
e.printStackTrace();
System.exit(-1);
}
cmds = new String[count];
for (count=0; count<cmds.length; count++) {
cmds[count] = sc.nextLine();
}
return cmds;
}
}