NYOJ题目889求距离

------------------------------------------

题目可以抽象一下为计算坐标系上两点间的距离,即

 

AC代码:

 1 import java.awt.Point;
 2 import java.io.BufferedReader;
 3 import java.io.IOException;
 4 import java.io.InputStreamReader;
 5 
 6 public class Main {
 7 
 8     public static void main(String[] args) throws IOException {
 9         
10         BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
11         
12         boolean first=true;
13         while(first || reader.ready()){
14             first=false;
15             String s1=reader.readLine();
16             String s2=reader.readLine();
17             double ans=solve(s1,s2);
18             System.out.printf("%.2f\n",ans);
19         }
20     }
21     
22     public static double solve(String s1,String s2){
23         Point p1=compile(s1);
24         Point p2=compile(s2);
25         return Math.sqrt(Math.pow(p1.x-p2.x,2)+Math.pow(p1.y-p2.y,2));
26     }
27     
28     public static Point compile(String s){
29         char cs[]=s.replaceAll(" ","").toCharArray();
30         Point p=new Point();
31         for(int i=0;i<cs.length;i++){
32             switch(cs[i]){
33             case 'W':
34                 p.x--;
35                 break;
36             case 'E':
37                 p.x++;
38                 break;
39             case 'S':
40                 p.y--;
41                 break;
42             case 'N':
43                 p.y++;
44                 break;
45             }
46         }
47         return p;
48     }
49     
50 }

 

题目来源: http://acm.nyist.net/JudgeOnline/problem.php?pid=889

转载于:https://www.cnblogs.com/cc11001100/p/5870458.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值