首页 > 代码库 > 《Crazy tea party》

《Crazy tea party》

Description

Download as PDF
 

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

3456

Sample Output

246

 

 题意:n个人顺时针围成一个圈;

   欲将其按逆时针围成圈;

   每次只能移动一对相邻的人;

   问至少需要移动多少次;

 

题解:由于这是一个环;

   将其折中取半来移动;

   最后便能使步骤最少;

 

代码:

  

 1 #include <stdio.h> 2 #include <iostream> 3 #include <cmath> 4 #include <algorithm> 5 #include <cstring> 6  7 using namespace std; 8  9 int main()10 {11    // freopen("ACM.txt","r",stdin);12     int t,n;13     cin>>t;14     while(t--)15     {16         int ans=0;17         cin>>n;18         int x=n/2;19         for(int i=1;i<=x;i++)20         {21             ans+=x-i;22         }23         for(int i=x+1;i<=n;i++)24         {25             ans+=n-i;26         }27         cout<<ans<<endl;28     }29 }
View Code

 

 

 

《Crazy tea party》