Sort it

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4679    Accepted Submission(s): 3250

Problem Description
You want to processe a sequence of n distinct integers by swapping two adjacent sequence elements until the sequence is sorted in ascending order. Then how many times it need.
For example, 1 2 3 5 4, we only need one operation : swap 5 and 4.
 
Input
The input consists of a number of test cases. Each case consists of two lines: the first line contains a positive integer n (n <= 1000); the next line contains a permutation of the n integers from 1 to n.
 
Output
For each case, output the minimum times need to sort it in ascending order on a single line.
 
Sample Input
3
1 2 3
4
4 3 2 1
 
Sample Output
0
6
 
 

题意很好理解,直接就想到冒泡排序了。

然而智障,在写的时候wa了一次。。。

代码:

#include<stdio.h>
int main()
{
int a[];
int i,j,t,n,ans;
while(~scanf("%d\n",&n)){
for(i=;i<n;i++)
scanf("%d",&a[i]);
ans=;
for(j=;j<n-;j++){ //是n-1,不是n。。。
for(i=;i<n-j-;i++) //是n-j-1,不是n-j,智障
if(a[i]>a[i+]){
t=a[i];
a[i]=a[i+];
a[i+]=t;
ans++;
}
}
printf("%d\n",ans);
}
return ;
}

HDU2689-Sort it-冒泡排序的更多相关文章

  1. HDU 5775 Bubble Sort(冒泡排序)

    p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...

  2. HihoCoder - 1781: Another Bubble Sort (冒泡排序&逆序对)

    Sample Input 3 9 8 7 5 1 9 2 6 4 3 1 2 3 4 5 6 7 8 9 9 8 7 5 1 9 2 6 4 3 1 2 5 4 3 6 7 8 9 9 8 7 5 1 ...

  3. Bubble Sort(冒泡排序)

    冒泡排序(英语:Bubble Sort,台湾另外一种译名为:泡沫排序)是一种简单的排序算法.它重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来.走访数列的工作是重复地进行 ...

  4. js中sort()方法冒泡排序模拟

    1.sort()方法概述 sort() 方法用于对数组的元素进行排序. 如果调用该方法时没有使用参数,将按字母顺序对数组中的元素进行排序,说得更精确点,是按照字符编码的顺序进行排序.要实现这一点, 首 ...

  5. 排序算法-冒泡排序(Bubble Sort)

    package com.wangzhu.sort; /** * 冒泡排序算法 * @ClassName: BubbleSort * @Description: TODO * @author wangz ...

  6. 【算法】冒泡排序(Bubble Sort)(一)

    冒泡排序(Bubble Sort) 冒泡排序是一种简单的排序算法.它重复地走访过要排序的数列,一次比较两个元素,如果它们的顺序错误就把它们交换过来.走访数列的工作是重复地进行直到没有再需要交换,也就是 ...

  7. STL sort

    STL的sort()算法,数据量大时采用Quick Sort,分段递归排序,一旦分段后的数据量小于某个门槛,为避免Quick Sort的递归调用带来过大的额外负荷,就改用Insertion Sort. ...

  8. MIPS—冒泡排序

    SORT 使用冒泡排序对整数数组进行排序,这种排序虽然不是最快的,但却是最简单的. C语言代码 #include<stdio.h> #include<iostream> usi ...

  9. 排序算法-冒泡排序(Java)

    package com.rao.sort; import java.util.Arrays; /** * @author Srao * @className BubbleSort * @date 20 ...

  10. STL sort源码剖析

    转载自:http://www.cnblogs.com/imAkaka/articles/2407877.html STL的sort()算法,数据量大时采用Quick Sort,分段递归排序,一旦分段后 ...

随机推荐

  1. 使用Libmicrohttpd搭建内嵌(本地)服务器

    Libmicrohttpd简介 GNU Libmicrohttpd是一个用来在项目中内嵌http服务器的C语言库,它具有以下几个非常鲜明的特点: C语言库,小而快. API非常简单,且都是可重入的. ...

  2. 【WebGL】《WebGL编程指南》读书笔记——第5章

    一.前言        终于到了第五章了,貌似开始越来越复杂了. 二.正文         Example1:使用一个缓冲区去赋值多个顶点数据(包含坐标及点大小) function initVerte ...

  3. android的ADK下载地址

    把下面所有的包下载到temp目录下进行安装. 用代理http://ppdaili.com/https://dl-ssl.google.com/android/repository/repository ...

  4. Java自己动手写连接池二

    读取数据库文件,来操作: package com.kama.cn; import java.sql.Connection;import java.sql.DriverManager;import ja ...

  5. flask连接sqlalchemy数据库,实现简单的登录跳转功能

    环境:python2.7 python库:flask,flask_wtf,wtforms,sqlalchemy 原理:运行app-连接数据库-打开登录页面-输入登录信息(错误->提示错误信息:正 ...

  6. ant安装和验证

    1.下载apache-ant-1.9.6 2.D:\software\apache-ant-1.9.6 3.配置环境变量 在系统变量path的最后面添加D:\software\apache-ant-1 ...

  7. eml企业通讯录管理系统v5.0 存在sql注入

    0x00 前言 上周五的时候想练练手,随便找了个系统下载下来看看. 然后发现还有VIP版本,但是VIP要钱,看了一下演示站,貌似也没有什么改变,多了个导入功能?没细看. 搜了一下发现这个系统,压根就没 ...

  8. Javascript一句代码实现JS字符串去除重复字符

    需求: 原字符串:abcdabecd 去重后字符串:abcde JS字符串去重,一个简单需求,网上找案例发现都是一大堆代码,对于强迫症的我 实再无法忍受,于是自己手动写出一段代码,完美解决该问题. 代 ...

  9. hexo next主题为博客添加分享功能

    title: hexo next主题为博客添加分享功能 date: 2018-01-06 20:20:02 tags: [hexo博客, 博客配置] categories: hexo next主题配置 ...

  10. AI时代:推荐引擎正在塑造人类

    We shape our tools and afterwards our tools shape us. ------Marshall McLuhan 麦克卢汉说:"我们塑造了工具,反过来 ...