zoukankan      html  css  js  c++  java
  • 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;
    }
    


  • 相关阅读:
    easyui好例子,值得借鉴
    DDL 和DML 区别
    兼容IE的文字提示
    搭代理
    美国服务器
    跟随滚动条滚动
    JS Array对象
    JS 内置对象 String对象
    JS 对象
    JS 二维数组
  • 原文地址:https://www.cnblogs.com/xindoo/p/3595029.html
Copyright © 2011-2022 走看看