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;
}
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;
}
相关文章推荐
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- LCL.VBS 病毒源代码
- jQuery-digest | 咀嚼jQuery源码
- [源码]Norton Ghost C# 源代码
- [源代码]symantec远程控制软件PcAnywhere源代码分享
- [源代码]Torque3D引擎SDK
- [游戏源码]Doom3公布源代码下载
- 《Doom 3》源代码将在《Rage》上市后免费发布
- 【资料分享】网游[天龙八部]源代码分享
- Android系统默认Home应用程序(Launcher)的启动过程源代码分析
- 65个源代码网站
- 一个游戏程序员的学习资料
- 剖析top命令显示的VIRT RES SHR值
- CentOS 6.3下Apache+SVN部署Web版本同步
- Python入门书籍(中文版)
- Linux下Samba源代码安装以及环境配置
- 开源硬件监控软件:Open Hardware Monitor
- Linux之父盟友分道扬镳 直言开源模式软肋
- Linux vs BSD我们到底选择谁!
- 嵌入式系统开发,不用Linux的理由 ! (转,这个有点牛!)