zoukankan      html  css  js  c++  java
  • java实现机器人行走

    某少年宫引进了一批机器人小车。可以接受预先输入的指令,按指令行动。小车的基本动作很简单,只有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);
        }
    }
    
  • 相关阅读:
    [二分][dp] Jzoj P3463 军训
    [树状数组] Jzoj P3462 休息
    [期望] Jzoj P3459 TheSwaps
    [dp] Jzoj P3460 Mixing Chemicals
    [数位dp][状压dp] Jzoj P3458 密码
    [匈牙利] Jzoj P1156 使命的召唤
    [bfs][状压] Jzoj P2121 分球
    [dp] Jzoj P1187 最大公共子串
    [树套树] Jzoj P5699 【gdoi2018 day1】涛涛接苹果
    [枚举] Jzoj P3387 终极武器
  • 原文地址:https://www.cnblogs.com/a1439775520/p/13077855.html
Copyright © 2011-2022 走看看