Knuthocean's teacher asked him to sort some numbers ascending and get the times of exchanges (exchanges happen only between two neighboring letters), can you help him?
输入格式
There are multiple test case, for each test case, the first line contain an integer n(n<10000), the second line contain n integers.
The last line of input contains a single number 0.
输出格式
Output the least times of the exchanges during the sort.
样例输入
5 1 2 3 4 5 4 1 3 2 4 0
样例输出
0 1
#include<stdio.h>
int a[10000];
int main(){
int n,i,j,ans,t;
while(scanf("%d",&n)){
if(n==0) break;
for(i=0;i<n;i++)
scanf("%d",&a[i]);
ans=0;
for(i=0;i<n-1;i++){
for(j=0;j<n-1-i;j++){
if(a[j]>a[j+1]){
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
ans++;
}
}
}
printf("%d\n",ans);
}
return 0;
}