AtCoder题解——Beginner Contest 178——B - Product Max

题目相关

题目链接

AtCoder Beginner Contest 178 B 题,https://atcoder.jp/contests/abc178/tasks/abc178_b

Problem Statement

Given are integers a,b,c and d. If x and y are integers and a≤x≤b and c≤y≤d hold, what is the maximum possible value of x×y?

Input

Input is given from Standard Input in the following format:

a b c d

Output

Print the answer.

Samples1

Sample Input 1

1 2 1 1

Sample Output 1

2

Explaination

If x=1 and y=1 then x×y=1. If x=2 and y=1 then x×y=2. Therefore, the answer is 2.

Samples2

Sample Input 2

3 5 -4 -2

Sample Output 2

-6

Samples3

Sample Input 3

-1000000000 0 -1000000000 0

Sample Output 3

1000000000000000000

Constraints

  • -10^{9}\leqslant a \leqslant b \leqslant10^{9}
  • -10^{9}\leqslant c \leqslant d \leqslant10^{9}
  • All values in input are integers.

题解报告

AtCoder 的 A 和 B 都是非常简单的题目,适合初学者。难度在于英文的理解能力上。

题目翻译

给定四个整数 a、b、c 和 d,现在有两个整数 x (a\leqslant x \leqslant b)和 y(c\leqslant y \leqslant d)。问 x*y 的最大值是多少?

题目分析

x*y 的最大值,而且 x 和 y 没有相关性,那么,只需要根据 x 和 y 的独立情况进行分类讨论即可。注意到 x 和 y 可能出现负数,使用分类讨论方法,需要特别仔细的分类。

换一个思路来看,我们求的最大值,因此只能出现在 a、b、c 和 d 这四个数字的乘积组合上。也就是说,最大值只能是 a*c、a*d、b*c 或者 b*d 这四个数中的一个。这样,问题就变成在四个数中找最大值。

AC 参考代码

//https://atcoder.jp/contests/abc178/tasks/abc178_b
#include <bits/stdc++.h>
using namespace std;
int main() {
    long long a,b,c,d;
    cin>>a>>b>>c>>d;
     
    long long ans;
    ans=max(a*c, a*d);
    ans=max(ans, b*c);
    ans=max(ans, b*d);
    cout<<ans<<"\n";
     
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

努力的老周

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值