20200112-曼哈顿距离



  1. #include "stdafx.h"

  2. #include "iostream"

  3. #include "cstring"

  4. #include "string"

  5. using namespace std;

  6. int a,b,c,d,e,f;

  7. int main()

  8. {cin>>a;

  9.  cin>>b;

  10.  cin>>c;

  11.  cin>>d;

  12.  if(a-c<0)

  13.  e=c-a;

  14.  else

  15.  e=a-c;

  16.  if(b-d<0)

  17.  f=d-b;

  18.  else

  19.  f=b-d;

  20.  cout<<e+f<<endl;}

  21. }

运行结果:


输入1,2,3,4

输出结果4
上述代码可简化为:
#include "stdafx.h"
#include "iostream"
#include "cstring"
#include "string"
using namespace std;
int x1,x2,z1,z2,e,f;//不要用y1,y2,因为"y1"在gcc(mingw)里面是一个内置函数。
int main()
{cin>>x1>>x2>>z1>>z2;
 if(z1-x1<0)
 e=x1-z1;
 else
 e=z1-x1;
 if(z2-x2<0)
 f=x2-z2;
 else
 f=z2-x2;
 cout<<e+f<<endl;}
 

下一题:

输出4
分析:
     求0-14中哪些数字的平方对15取余的结果=1,分别是1,4,11,14
posted @ 2020-05-21 08:34  财盛  阅读(106)  评论(0编辑  收藏  举报