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
     
  • 相关阅读:
    Expect学习笔记(1)
    Awk 实例,第 3 部分
    ELF文件格式(中文版)
    sed 实例,第 1 部分
    Expect 教程中文版
    csc工具一般使用说明zz
    Microsoft Office Word 2010(zz)
    记录XPO查询 日志
    C#中判断文件或文件夹是否存在
    Discuz数据库结构1
  • 原文地址:https://www.cnblogs.com/M-D-LUFFI/p/4084312.html
Copyright © 2011-2022 走看看