Manhattan distance(for lab)

Input four integer x1, y1, x2, y2, which is mean that the coordinates of two points A(x1, y1), B(x2, y2). [0 <= x1,y1,x2,y2 <= 10000]

Please output manhatton distance between the two points.

 

output format:there is an "\n" behind the manhatton distance.

 

For example

[Input]

0 0 1 1

 

[Output]

2

 

 

hint

Manhattan distance, considered by Hermann Minkowski in 19th century Germany, is a form of geometry in which the usual distance function of metric or Euclidean geometry is replaced by a new metric in which the distance between two points is the sum of the absolute differences of their Cartesian coordinates.                 ——Find in Wiki

d(i,j)=|X1-X2|+|Y1-Y2|.

 

if you want to use the abs() function to calculate the absolute value, you should include the stdlib.h head file.

You also can compare X1 and X2.And then use the big value to minus the small value.

 

Input--> use scanf function

Output--> user printf function

tpis:Just output the manhatton distance and "\n". You do not need to output other information, especially Chinese string.

 

 

1.#include <stdio.h>
2.#include <stdlib.h>
3. 
4.int main() {
5.    int x1, y1, x2, y2;
6.    int sum = 0;
7. 
8.    scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
9.    sum = abs(x1 - x2) + abs(y1 - y2);
10.    printf("%d\n", sum);
11. 
12.    return 0;
13.}

要注意的就是求绝对值用abs(),需要头文件<stdlib.h>

posted on 2016-01-14 00:11  _阿赞  阅读(535)  评论(0编辑  收藏  举报