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 thestdlib.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>

优质内容筛选与推荐>>
1、Java连接本地MySQL数据库进行增删改查操作
2、function core分析
3、java读取动态日志文件
4、Run!——方便你随时在当前页面编辑 & 运行js代码的chrome扩展
5、最小二乘法多项式曲线拟合原理与实现


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号