AGC 008 A - Simple Calculator

http://www.elijahqi.win/archives/1591
Time limit時間制限 : 2sec / Memory limitメモリ制限 : 256MB

配点 : 300 点

問題文
すぬけ君は電卓を持っています。 この電卓にはディスプレイと 2 個のボタンが付いています。

最初、ディスプレイの値は整数 x です。 すぬけ君の目標は、ディスプレイの値を整数 y にすることです。 そのために、すぬけ君は次の 2 個のボタンを好きな順番で何回か押すことができます。

ボタン A : ディスプレイの値を 1 増やす。
ボタン B : ディスプレイの値の符号を反転する。
目標を達成するためにすぬけ君がボタンを押す回数の最小値を求めてください。 なお、整数 x, y の値によらず、必ず目標を達成できることが示せます。

制約
x, y は整数である。
|x|,|y|≤109
x, y は相異なる。
入力
入力は以下の形式で標準入力から与えられる。

x y
出力
目標を達成するためにすぬけ君がボタンを押す回数の最小値を出力せよ。

入力例 1
Copy
10 20
出力例 1
Copy
10
ボタン A を 10 回押せばよいです。

入力例 2
Copy
10 -10
出力例 2
Copy
1
ボタン B を 1 回押せばよいです。

入力例 3
Copy
-10 -20
出力例 3
Copy
12
次の順でボタンを押せばよいです。

ボタン B を 1 回押す。
ボタン A を 10 回押す。
ボタン B を 1 回押す。
Score : 300 points

Problem Statement
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.
Input
The input is given from Standard Input in the following format:

x y
Output
Print the minimum number of times Snuke needs to press the buttons to achieve his objective.

Sample Input 1
Copy
10 20
Sample Output 1
Copy
10
Press button A ten times.

Sample Input 2
Copy
10 -10
Sample Output 2
Copy
1
Press button B once.

Sample Input 3
Copy
-10 -20
Sample Output 3
Copy
12
Press the buttons as follows:

Press button B once.
Press button A ten times.
Press button B once.
按题意模拟 的简单题我竟然wa了这么多 果然还是太菜了
注意几点 :要根据 a b的正负形 还有他们的绝对值大小判定

#include<cstdio>
#include<cmath>
long long a,b;
using namespace std;
int main(){
//  freopen("agc.in","r",stdin);
    scanf("%lld%lld",&a,&b);long long ans=0;
    if (a<0&&b<0&&a>=b) {ans+=2;ans+=abs(a-b);}
    if (a<0&&b<0&&a<b) {ans+=abs(a-b);}
    if (a<0&&b>0) {ans=1;ans+=abs(b+a);}
    if (a>0&&b<0) {ans=1;ans+=abs(b+a);}
    if (a>0&&b>0&&a<=b) {ans+=abs(b-a);}
    if (a>0&&b>0&&a>b) {ans=2;ans+=abs(b-a);}
    if (!a||!b) {
        if (a>b) ans+=abs(b-a)+1;else ans+=abs(b-a);
    }
    printf("%lld",ans);
    return 0;
}

zhx大爷的orz%% 简单明了的写法

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
inline int read(){
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    return x*f;
}
int x,y,ans=0;
inline int abs(int x){return x<0?-x:x;}
int main(){
//  freopen("a.in","r",stdin);
    x=read();y=read();
    if(x==-y) ans=1;
    else if(abs(x)<abs(y)) ans=abs(y)-abs(x)+(x<0)+(y<0);
    else ans=abs(x)-abs(y)+(x>0)+(y>0);
    printf("%d\n",ans);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/elijahqi/article/details/80466192
008