nyoj-692-Chinese checkers【思维题】【好题】

17 篇文章 0 订阅
4 篇文章 0 订阅

nyoj-692-Chinese checkers


描述
I think almost everyone play Chinese checkers when we were young.

这里写图片描述

Recently, hft777 is fond of playing Chinese checkers. But he is puzzled with how many steps at least to move all his pieces to his opposite position.
To make the problem simple, now we take a line into account. There are n positions on a line, numbered from 0 to n-1.At fast, two same balls is on 0 and 1 position, and every time you can move a ball left or right, or you can move a ball on the position x to the position z on the other side of the ball on the position y, and |x-y|=|y-z|. And you must mark sure the ball can’t move out of the bounder, and the two balls can’t be on one same position. Now, hft777 wants to how many steps at least to move the two same to the n-2, n-1 positions.

输入
The first line of the input is one integer t, the number of testcase.
For each testcase, one integer n for a line, corresponding to the length of the line. 2≤n<1000
输出
For each testcase output the minimum steps.

样例输入
2
3
4
样例输出
1
2

题目链接:nyoj-692

题目大意:在一条直线上,0,1上面有两个点,终点为n-1,n-2.

移动方式有两种:
1. 左移或右移一格
2. 假设x在y的左边。x能一步跳到z(z - y = y - x)

题目思路:
这里写图片描述

  1. 设a点为左边的点,b点为右边的点
  2. 第一步:将点b移动到等于步长的位置
  3. 每次移动一次,相当于把a,b整体往左边移动一个步长,直到a点位于终点上或者终点前最优位置(即a点位置<=n-2的最大值)
  4. 最后处理,两个点移动到n-1和n-2所需步数,一定为a-1

以下是代码:

#include <iostream>
using namespace std;
int main()
{
    int t;
    cin >> t;
    while(t--)
    {
        int n;
        cin >> n;
        int ans = 999999;
        for (int a = 1; a <= n / 2; a++)
        {
            int temp = (n - 2) /  a + (a - 1) * 2;
            ans = min(ans,temp);
        }
        cout << ans << endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值