某少年宫引进了一批机器人小车。可以接受预先输入的指令,按指令行动。小车的基本动作很简单,只有3种:左转(记为L),右转(记为R),向前走若干厘米(直接记数字)。

例如,我们可以对小车输入如下的指令:

15L10R5LRR10R20

则,小车先直行15厘米,左转,再走10厘米,再右转,…

不难看出,对于此指令串,小车又回到了出发地。

你的任务是:编写程序,由用户输入指令,程序输出每条指令执行后小车位置与指令执行前小车位置的直线距离。

【输入、输出格式要求】

用户先输入一个整数n(n<100),表示接下来将有n条指令。

接下来输入n条指令。每条指令只由L、R和数字组成(数字是0~100之间的整数)

每条指令的长度不超过256个字符。

程序则输出n行结果。

每条结果表示小车执行相应的指令前后位置的直线距离。要求四舍五入到小数后2位。

例如:用户输入:

5

L100R50R10

3LLL5RR4L12

LL

100R

5L5L5L5

则程序输出:

102.96

9.06

0.00

100.00

0.00

【注意】

请仔细调试!您的程序只有能运行出正确结果的时候才有机会得分!

package com.liu.ex4;

import java.util.Scanner;

public class Main {
public static int[] position = {0,1,2,3}; //表示机器人朝向,分别为上、左、下、右 public String getResult(String A) {
//机器人起始朝向默认为向上
int area = position[0];
double x = 0, y = 0;
for(int i = 0;i < A.length();i++) {
String temp = "";
if(A.charAt(i) == 'L') {
area = (area + 1) % 4;
} else if(A.charAt(i) == 'R') {
if(area == 0)
area = 3;
else
area = area - 1;
} else {
for(;i < A.length();i++) {
if(A.charAt(i) == 'L' || A.charAt(i) == 'R') {
i = i - 1;
break;
}
temp += A.charAt(i);
}
int num = Integer.valueOf(temp);
if(area == 0)
y = y + num;
else if(area == 1)
x = x - num;
else if(area == 2)
y = y - num;
else if(area == 3)
x = x + num;
}
}
double result = x * x + y * y;
result = Math.sqrt(result);
String tempResult = String.format("%.2f", result);
return tempResult;
} public void printResult(String[] A) {
String[] result = new String[A.length];
for(int i = 0;i < A.length;i++) {
result[i] = getResult(A[i]);
}
for(int i = 0;i < A.length;i++)
System.out.println(result[i]);
return;
} public static void main(String[] args) {
Main test = new Main();
Scanner in = new Scanner(System.in);
int n = in.nextInt();
in.nextLine();
String[] A = new String[n];
for(int i = 0;i < n;i++)
A[i] = in.nextLine();
test.printResult(A);
}
}

最新文章

  1. Oracle存储过程语法
  2. 360浏览器下载excel问题解决方式
  3. Python interview preparing
  4. Codeforces Beta Round #10 B. Cinema Cashier (树状数组)
  5. 2014年度辛星全然解读html第七节
  6. 有一个很大的整数list,需要求这个list中所有整数的和,写一个可以充分利用多核CPU的代码,来计算结果(转)
  7. mac版破解office
  8. 电视盒子好用又强大的跨屏远程输入法 TVRemoteIME
  9. linux下mycat自启动方法
  10. Leetcode(一)两数之和
  11. Centos 7 关闭selinux and firewall
  12. 可由inetd启动的协议无关时间获取服务器程序
  13. 【二次开发】shopxo商城
  14. RabbitMQ常见错误2
  15. function module 调用类对象
  16. Lunx下 怎样启动和关闭oracle数据库
  17. logging模块知识点及应用小结
  18. MTK 锁屏配置
  19. react使用阿里字体图标
  20. IDEA 搭建授权服务器

热门文章

  1. Mysql 常用函数(6)- replace 函数
  2. layui编辑商品时,怎么使用下拉菜单显示商品默认分类的问题
  3. android 防止多次点击,导致事件监听响应到其他界面
  4. 华为Mate8手机优化技巧
  5. iterm 分屏切换快捷键与配色设置
  6. 解决使用IDEA启动Tomcat成功但localhost:8080无法访问的问题
  7. 10个典型的JavaScript面试题
  8. Argo 项目加入 CNCF 孵化器 | 云原生生态周报 Vol. 45
  9. vue移动端转场动画
  10. NOIP 2017 P3959 宝藏 (状态压缩DP板子)