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

    3
    4
    5
    6
    

    Sample Output

    2
    4
    6
    

     题意: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
     
  • 相关阅读:
    export和import实现模块化
    Net Core
    DockerCon 2016
    NET Core 构成体系
    Cloud Engine
    JVM内存结构
    Signalr
    Rabbit.Rpc
    遍历指定包名下所有的类(支持jar)(转)
    httpd的简单配置(转)
  • 原文地址:https://www.cnblogs.com/M-D-LUFFI/p/4084312.html
Copyright © 2011-2022 走看看