码迷,mamicode.com
首页 > 其他好文 > 详细

《Crazy tea party》

时间:2014-11-09 00:56:48      阅读:241      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   io   color   ar   os   sp   

Description

bubuko.com,布布扣
 

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个人顺时针围成一个圈;

   欲将其按逆时针围成圈;

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

   问至少需要移动多少次;

 

题解:由于这是一个环;

   将其折中取半来移动;

   最后便能使步骤最少;

 

代码:

  

bubuko.com,布布扣
 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》

标签:des   style   blog   http   io   color   ar   os   sp   

原文地址:http://www.cnblogs.com/M-D-LUFFI/p/4084312.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!