A. Vasya and Book ( Codeforces Educational Codeforces Round 55 )

题意:当前在看书的第 x 页,每次可以向前或者向后翻 d 页,这个书一共 n 页,问能否用最小操作翻到第 y 页。

题解:三种情况:1、直接翻能到的一定最短。 2、先翻到第一页,然后往后翻,翻到第 y 页。3、先翻到第 n 页,然后往前翻,翻到第 y 页。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

int main()
{
   int ans,t,n,x,y,d;
   cin >> t;
   while(t--)
   {
        ans = 0;
        cin >> n >> x >> y >> d;
        if(abs(x-y) %d == 0)
        {
            ans = abs(x-y) / d;
            printf("%d\n",ans);
            continue;
        }
       else {
            int x1;
            if(x % d == 0) x1 = x /d;
            else x1 = x / d +1;
            if((y-1) % d == 0)
            {
                x1 += (y-1) / d;
            }
            else x1 = -1;
            int x2;
            if(abs(n - x) % d == 0) x2 = abs(n - x) / d;
            else x2 = abs(n - x) / d + 1;
            if((n-y)%d==0)
            {
                x2 += (n-y)/d;
            }
            else x2 = -1;
            if(x1 == -1 && x2 == -1) ans = -1;
            else if(x1 == -1 && x2 >= 0) ans = x2;
            else if(x2 == -1 && x1 >= 0) ans = x1;
            else ans = min(x1,x2);
            printf("%d\n",ans);
        }
   }

    return 0;
}

 

转载于:https://www.cnblogs.com/lcchy/p/10139395.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值