Snuke has a calculator. It has a display and two buttons.
Initially, the display shows an integer x. Snuke wants to change this value into another integer y, by pressing the following two buttons some number of times in arbitrary order:
Button A: When pressed, the value on the display is incremented by 1.
Button B: When pressed, the sign of the value on the display is reversed.
Find the minimum number of times Snuke needs to press the buttons to achieve his objective. It can be shown that the objective is always achievable regardless of the values of the integers x and y.
Constraints
x and y are integers.
|x|,|y|≤109
x and y are different.
Initially, the display shows an integer x. Snuke wants to change this value into another integer y, by pressing the following two buttons some number of times in arbitrary order:
Button A: When pressed, the value on the display is incremented by 1.
Button B: When pressed, the sign of the value on the display is reversed.
Find the minimum number of times Snuke needs to press the buttons to achieve his objective. It can be shown that the objective is always achievable regardless of the values of the integers x and y.
Constraints
x and y are integers.
|x|,|y|≤109
x and y are different.
enter
The input is given from Standard Input in the following format:
x y
x y
output
Print the minimum number of times Snuke needs to press the buttons to achieve his objective.
Water problems, clear thinking is generally not wrong.
The meaning of the question; give you two numbers x and y, there are two operations: operation (1) the value of x +1, operation (2) x=-x; ask you how many steps you take at least.
The idea is relatively simple, do not write
#include<stdio.h>
#include<math.h>
#include<string.h>
using
namespace
std;
#define ll long long
ll min(ll a,ll b ) {
return
a<b?a:b;}
ll max(ll a,ll b) {
return
a>b?a:b;}
ll minn(ll a,ll b,ll c){
return
min(min(a,b),min(b,c));}
ll maxn(ll a,ll b,ll c){
return
max(max(a,b),max(b,c));}
int
main()
{
int
x,y;
scanf
(
"%d%d"
,&x,&y);
if
(x==-y)
printf
(
"1\n"
);
else
if
(x==0) {
if
(y>0)
printf
(
"%d\n"
,y);
else
printf
(
"%d\n"
,-y+1);
}
else
if
(y==0)
{
if
(x<0)
printf
(
"%d\n"
,-x);
else
printf
(
"%d\n"
,x+1);
}
else
if
(0<x&&x<y)
printf
(
"%d\n"
,y-x);
else
if
(0<y&&y<x)
printf
(
"%d\n"
,x-y+2);
else
if
(x>0&&y<0)
{
if
(x>-y)
printf
(
"%d\n"
,x+y+1);
else
if
(x<-y)
printf
(
"%d\n"
,-y-x+1);
}
else
if
(x<0&&y>0)
{
if
(-x<y)
printf
(
"%d\n"
,y+x+1);
else
printf
(
"%d\n"
,-x-y+1);
}
else
if
(x<y&&y<0)
printf
(
"%d\n"
,y-x);
else
if
(y<x&&x<0)
printf
(
"%d\n"
,x-y+2);
return
0;
}
|