poj 1455 Crazy tea party

    这道题第一眼看去很难,其实不然,短短几行代码就搞定了。

    说一下大概思路,如果是排成一排的n个人,如 1 2 3 4 5 6 7 8 我们要变成 8 7 6 5 4 3 2 1 需要交换 28次,找规律的话就是 n*(n-1)/2,但这道题是一个圈,要让他们顺序变反的话不一定1要在8的位置上去,4 3 2 1 8 7 6 5 这样也是反的,我们只要把n个人分成两部分,然后按拍成一条线的方法来出来两部分就OK了;

#include <iostream>
#include <stdio.h>

using namespace std;

int main()
{
    int n, t;
    cin >> t;
    while (t--)
    {
        scanf("%d", &n);
        int x = n>>1;
        x -= 1;
        int ans = x*(x+1)/2;
        if (n&1)
            ans = ans + (x+1)*(x+2)/2;
        else
            ans = ans<<1;
        printf("%d
", ans);
    }
    return 0;
}


原文地址:https://www.cnblogs.com/xindoo/p/3595029.html