题:

计算机画图时,有点的概念,每个点由它的横坐标x 和 纵坐标 y 描述。

写一个类。

求两个点之间的曼哈顿距离 = 横向距离 + 纵向距离

例如,一个点(0,0) 和另一个点(1,1)的曼哈顿距离为2

package test;

public class Point
{ public Point()
{
// TODO Auto-generated constructor stub
} public int x;
public int y; public int manhattan(Point n)
{
int dx = Math.abs(n.x - this.x);
int dy = Math.abs(n.y - this.y);
return dx + dy;
}
} package test; public class test
{ public test()
{
// TODO Auto-generated constructor stub
} public static void main(String[] args)
{
// TODO Auto-generated method stub
Point p1 = new Point();
p1.x = 4;
p1.y = 5;
Point p2 = new Point();
p2.x = 3;
p2.y = 5;
int d = p1.manhattan(p2);
System.out.println(d); } }
04-26 18:07
查看更多