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

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)

题目思路:
nyoj-692-Chinese checkers【思维题】【好题】_第1张图片

  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;
}

你可能感兴趣的:(nyoj,692)