鏈接:https://ac.nowcoder.com/acm/contest/368/A
來源:牛客網
走方格
時間限制:C/C++ 1秒,其他語言2秒
空間限制:C/C++ 131072K,其他語言262144K
64bit IO Format: %lld
題目描述
在一個n*n的方格中,你只能斜着走。
你還有一次上下左右走的機會
給你一個起點(sx,sy),和終點(ex,ey),詢問從起點到終點最少走多少步。
輸入描述:
一行5個整數,n,sx,sy,ex,ey。
1 \leq sx,sy,ex,ey \leq n \leq 10^{18}
輸出描述:
一行一個整數,表示從起點到終點最少走多少步。
示例1
輸入
複製
8 2 3 7 5
輸出
複製
5
思路:水題,不多解釋。
AC代碼O(1):
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
BigInteger n=sc.nextBigInteger();
BigInteger sx=sc.nextBigInteger();
BigInteger sy=sc.nextBigInteger();
BigInteger ex=sc.nextBigInteger();
BigInteger ey=sc.nextBigInteger();
BigInteger tmp1=ex.subtract(sx);
BigInteger tmp2=ey.subtract(sy);
BigInteger m1=new BigInteger("0");
BigInteger m2=new BigInteger("-1");
if(tmp1.compareTo(m1)<0)
tmp1=tmp1.multiply(m2);
if(tmp2.compareTo(m1)<0)
tmp2=tmp2.multiply(m2);
if(tmp1.compareTo(tmp2)>0)
System.out.println(tmp1);
else
System.out.println(tmp2);
sc.close();
}
}