1930. 排序
来源:互联网 发布:用友软件培训班 编辑:程序博客网 时间:2024/05/16 01:21
1930. 排序
Constraints
Time Limit: 1 secs, Memory Limit: 32 MB
Description
我们都知道,在排序算法中需要进行多次比较。若比较的两个数Ai, Aj(i<j),Ai>Aj的话,则称作有效比较。给定一个有N个互异元素的整数序列,请使用选择排序对序列进行排序,并输出其有效比较次数。这里假设使用的是标准的排序算法,并且从下标小到大的顺序进行枚举。
比如,给定序列[4,1,3,2],选择排序的有效比较按顺序有(4,1), (4,3), (3,2)。
Input
第一行为一个整数t,表示测试用例个数。对于每个测试用例,第一行包含一个整数n(0<n<100),表示序列的元素个数。接下来一行包含n个互不相同的整数,表示序列的元素。
Output
为每个测试用例单独输出一行表示使用标准的选择排序的有效比较次数。
Sample Input
244 1 3 233 2 1
Sample Output
32
// Problem#: 1930// Submission#: 1995867// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/// All Copyright reserved by Informatic Lab of Sun Yat-sen University#include<iostream>#include<string>using namespace std; int main(){ int t; cin>>t; while(t--){ int n; cin>>n; int *p=new int[n]; int i; for(i=0;i<n;i++){ cin>>p[i]; } int counter=0; int key; for(i=0;i<n-1;i++){ int flag=i; for(key=i+1;key<n;key++){ if(p[key]<p[flag]){ flag=key; counter++; } } if(flag!=i){ int temp=p[i]; p[i]=p[flag]; p[flag]=temp; } } cout<<counter<<endl; } return 0;}
- 1930. 排序
- 1930. 排序
- sicily--1930. 排序
- sicily 1930.排序
- Sicily 1930. 排序
- sicily-1930.排序
- 排序
- 排序
- 排序
- 排序
- 排序
- 排序
- 排序
- 排序
- 排序
- 排序
- 排序
- 排序
- Ubuntu 12.04 Eclipse 添加快速启动项
- WPF多窗口综合案例
- Linux 默认命令失效
- GMT和UTC的区别
- ARM在嵌入式linux内核裁剪与移植的应用
- 1930. 排序
- 尿床患儿母来信照登:漫漫求医路
- 仿函数(functor function objects)
- 数学专项counting:UVa 10079
- 【JAX-WS入门系列】第03章_使用SAAJ创建SOAP消息
- hdu 2057 命运
- C的一些知识【持续更新】
- 黑马程序员_编程基础
- CMOS Sensor的调试经验