codeforces Round #568 div2-01

题意:

       在一条直线上存在三个点(有坐标),并给出一个距离d,求至少移动多少个单位(可左可右)才能使得两两点之间的距离不小于该距离。

思路:

      因为三个点是没有身份或者先后区别的,所以首先对三个点进行排序,得出最小的点和中间点的距离d_1,和最大点和中间点的距离d_2,那么只要这两个距离满足,就显然满足了。那么就存在下面的情况

     1、d_1 > = d  && d_2 < d,那么只要移动最大点(继续变大)

     2、d_2 > = d  && d_1 < d,那么只要移动最小点(继续变小)

     3、d_1  <  d  && d_2  <  d,那么需要同时移动最大(变大)和最小点(变小)

     4、d_1 > = d  && d_2 >= d,那么需要移动次数为 0

代码:

#include<iostream>
#include <algorithm>
using namespace std;
int Array[3];
int main(){
    int d;
    int temp_1, temp_2;
    while(cin >> Array[0] >> Array[1] >> Array[2] >> d){
        if(Array[0] < 1 || Array[1] < 1 || Array[2] < 1 || d < 1) return 0;
        sort(Array,Array+3); 
        temp_1 = abs(Array[1] - Array[0]);
        temp_2 = abs(Array[2] - Array[1]);
        if(temp_1 < d && temp_2 < d){
            cout << (2*d - temp_1 - temp_2) << endl;
            continue;
        }
        if(temp_1 < d && temp_2 >= d){
            cout << (d - temp_1) << endl;
            continue;            
        }
        if(temp_1 >= d && temp_2 < d){
            cout << (d - temp_2) << endl;
            continue;
        }
        cout << 0 << endl;
    }
    return 0;
    
}  

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值