蓝桥杯(机器人)


打包语句
package ban.com;

引入相关包
import java.text.DecimalFormat;
import java.util.Scanner;
public class test {

方向变量代表方向
public static int direction ;

方向数组这点很巧妙画图理解
public static int X[]={1,0,-1,0};
public static int Y[]={0,-1,0,1};

这一点的位置
public static int x,y;

 

获得当前的方向
public static void GetDirection(String d){


if (d.equals("L")){
direction = (direction +1)%4;
}

else if (d.equals("R")){
direction = (direction +3)%4;
}

}

 

通过该函数我们就能化微观为宏观
public static void CountXY(int d){
x += X[direction]*d;
y += Y[direction]*d;
}

 


public static void getAnswer(String s){
int i,j,k;
String ss = "";
k = s.length();

遍历模拟进行求解
for(i = 0;i<k;i++){
char c = s.charAt(i);
if (c >= '0' && c <= '9'){
ss += c;
}

else {
if (!ss.equals("")){
CountXY(Integer.valueOf(ss));
ss = "";
}
GetDirection(c+"");
}
}

最后是数字的情况
if (!ss.equals("")){
CountXY(Integer.valueOf(ss));
ss = "";
}

}
public static void main(String[] args) {
String s;
int n;

基本输入输出
Scanner sc = new Scanner(System.in);

申明浮点数
DecimalFormat dg=new DecimalFormat("0.00"); //
保留两位小数

基本的输入
n = sc.nextInt();
while( n > 0){

进行初始化
direction = 0;
x = 0;
y = 0;
s = sc.next();
getAnswer(s);

最后将用宏观的思维方法来解释
System.out.println(dg.format(Math.sqrt(x*x+y*y)));
n --;
}
}
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值