您的位置:首页 > 编程语言

Crazy Tea Party

2013-10-05 12:16 337 查看
n participants of "crazy tea party" sit around the table. Each minute one pair of neighbors can change their places. Find the minimum time (in minutes) required for all participants to sit in reverse
order (so that left neighbors would become right, and right - left).
Input
The first line is the amount of tests. Each next line contains one integer n (1 ≤ n ≤ 32767) - the amount of crazy tea participants.
Output
For each number n of participants to crazy tea party print on the standard output, on a separate line, the minimum time required for all participants to sit in reverse order.

Sample Input
3

4

5

6

Sample Output
2

4

6

提议概述:
              n个人按编号围着桌子坐成一圈,编号由1-n,每次只能交换相邻座位的人的位置,问多少次可以将所有人的左右两人互换,即左边的人到他的右边,右边的人到他的左边。

解题思路:
              如果n个人按编号站成一排,则此题就是一个冒泡排序,如:1,2,3,4,5变为5,4,3,2,1即可,(n/2)*(n/2-1)/2;
              如果n个人围桌坐成一圈,则需要将人分为两组,这两组各自形成一个列,用冒泡排序分别处理两组人即可,最后将结果相加。若n为偶数,则为(n/2)*(n/2-1);若为奇         数,则为(n/2)*(n/2-1)/2+(n/2+1)*(n/2)/2;

源代码:

#include<iostream> 

using namespace std;  

  

int main()  

{  

    int T,N,M;  

    cin>>T;  

   
while(T--)  

    {  

        cin>>T;    

        if(N%2==0)  

        {  

            M=(N/2)*(N/2-1);  

        }  

        else  

        {  

            M=(N/2)*(N/2-1)/2+(N/2+1)*(N/2)/2;  

        }  

        cout<<M<<endl;  

    }  

    return 0;

}  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ACM编程 源代码