啊,推等差快推傻了,这题目好烦
Java 代码
import java.util.Scanner;
public class 螺旋折线 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int x = input.nextInt();
int y = input.nextInt();
if (x == 0 && y == 0){
System.out.println(0);
return;
}
if (y > 0 && y >= Math.abs(x)){
int l = y;
long start = 2 + (4 * l + 2) * (l - 1);
int add = x + y;
long res = start + add;
System.out.println(res);
}else if (x > 0 && x >= Math.abs(y)){
int l = x;
long start = 2 + (4 * l + 2) *( l - 1) + 2 * l;
long add = l - y;
long res = start + add;
System.out.println(res);
}else if (y <= 0 && Math.abs(y) >= Math.abs(x + 1)){
int l = -y;
long start = (4 * l + 2) * l;
long add = l - x;
long res = start + add;
System.out.println(res);
}else {
int l = -x;
long start = 1 + 4 * l *(l -1);
long add = y - x - 1;
long res = start + add;
System.out.println(res);
}
}
}